Help JQuery-Ajax File Upload

Discussion in 'Programming' started by mikaradmall, Jan 21, 2012.

  1. #1
    Hi,

    I need help on how I could upload .doc files using jQuery and Ajax to a folder in server. Below is my code:

    HTML:
    <input type="file" name="filename" id="filename">

    JQuery
    jQuery.ajax({
    type: "POST",
    url: "upload.aspx",
    data: "filename=" + $("#filename").val(),
    success: function(msg){
    //do something
    }
    })

    The upload.aspx contains the code to copy the file and save it on server. The problem is it is only getting the filename. I can't get the contents of the file. Kindly advise as my code above doesn't work. I need this done through JQuery-Ajax since I have scripts to run after the file upload.

    Thanks so much.
     
    mikaradmall, Jan 21, 2012 IP
  2. CafeDude

    CafeDude Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello! So you want to save the file to filesystem and then read it's contents? If so you need to save first and then read the Stream from the saved file. This would be done on the server with your upload.aspx code which would be in C# or VB. C# I hope : )
     
    CafeDude, Jan 21, 2012 IP
  3. mikaradmall

    mikaradmall Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Yes, I want to save it to the filesystem. The only problem is the jquery data (data: "filename=" + $("#filename").val()) is only passing the filename to upload.aspx. I need to get the full filepath. How do I make it work (same as form submit) but using the JQuery-Ajax side (since I has scripts to process after file upload)?
     
    mikaradmall, Jan 21, 2012 IP
  4. CafeDude

    CafeDude Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Are you coding this from scratch or have something out-of-box? The JQuery-Ajax should post the file stream to your upload.aspx page. You won't be able to get the path for security reasons from the client. Ajax-Uploads can be tricky.
     
    CafeDude, Jan 21, 2012 IP
  5. mikaradmall

    mikaradmall Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    I'm coding it from scratch. I tried testing it with the ordinary form submit and it worked just fine. But when I transferred it to jQuery-Ajax, it only gets the filename and doesn't save the file on server. I need this done through jQuery though because I have scripts to run after creating the file. Is there any way I could upload files with jQuery-Ajax and with my upload .aspx?
     
    mikaradmall, Jan 21, 2012 IP
  6. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    guardian999, Jan 21, 2012 IP
  7. CafeDude

    CafeDude Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I sent you PM
     
    CafeDude, Jan 21, 2012 IP
  8. mikaradmall

    mikaradmall Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    Replied to your PM. Thanks.
     
    mikaradmall, Jan 22, 2012 IP