shawleigh17
shawleigh17

Reputation: 1187

Multiple File Upload using JQuery and C#

I am trying to perform a file upload using JQuery and c#, but I am having a lot of problems. I can't figure out how to get the file to upload. It works fine if I use FileUpload, but I am allowing the user to dynamically add files, and so I am using Jquery (ajax post to another file) in order to process the file. First I was getting a POST and enctype error, but now it just isn't doing anything. Firebug is showing me that the ajax code is failing, but doesn't tell me anything else.

Here is my jquery:

    function AddDocumentsDatabase() {
    $('input:file').each(function (index) {
        var fileName = $(this).val();

        $.ajax(
        {
            type: "POST",
            url: "../ajaxURLs/InsertDocument.aspx?requestNumber=" + reqNum + "&fileName=" + fileName,
            contentType: 'multipart/form-data',
            cache: false,
            success: function (html) {
                alert("File Inserted!")
            }
        }
        );
    });
}

And here is the InsertDocument.aspx code:

RequestDB db = new RequestDB();
    ApplicationFunctions app = new ApplicationFunctions();

    protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello");
    foreach (string key in Request.Form)
    {
        if (!key.StartsWith("fleBrowse")) continue;
        {
            Response.Write(Request.Form[key].GetType());
        }

    }
}

If it is something really easy, I apologize, my mind isn't running at full speed, right now. Any suggestions are greatly appreciated.

Upvotes: 1

Views: 3042

Answers (2)

Krunal Mevada
Krunal Mevada

Reputation: 1655

try this one which given blow link :

http://www.dotnetcurry.com/ShowArticle.aspx?ID=317

Upvotes: 0

Ethan Brown
Ethan Brown

Reputation: 27292

I agree with shawleigh17; it's a big job, and others have done it well. I've used jQuery FileUpload (http://blueimp.github.com/jQuery-File-Upload/) with great success. However, if you do want to try to debug your code, try adding an error function to your ajax call to debug:

$.ajax(
{
  type: "POST",
  url: "../ajaxURLs/InsertDocument.aspx?requestNumber=" + reqNum + "&fileName=" + fileName,
  contentType: 'multipart/form-data',
  cache: false,
  success: function (html) {
    alert("File Inserted!")
  },
  error(jqXHR, textStatus, errorThrown) {
    alert( "Error: " + textStatus + ": " + errorThrown );
  }
});

Upvotes: 2

Related Questions