File Upload with jQuery AJAX and Handler (ashx) not working

后端 未结 2 752
日久生厌
日久生厌 2021-01-01 04:08

I\'m trying to upload an image file with jQuery AJAX and a generic handler. But it seems that the file isn\'t being passed to the handler. After submit context.Request

相关标签:
2条回答
  • 2021-01-01 04:33

    Managed to get this working :)

    Here's my code...

    ///create a new FormData object
    var formData = new FormData(); //var formData = new FormData($('form')[0]);
    
    ///get the file and append it to the FormData object
    formData.append('file', $('#file')[0].files[0]);
    
    ///AJAX request
    $.ajax(
    {
        ///server script to process data
        url: "fileupload.ashx", //web service
        type: 'POST',
        complete: function ()
        {
            //on complete event     
        },
        progress: function (evt)
        {
            //progress event    
        },
        ///Ajax events
        beforeSend: function (e) {
            //before event  
        },
        success: function (e) {
            //success event
        },
        error: function (e) {
            //errorHandler
        },
        ///Form data
        data: formData,
        ///Options to tell JQuery not to process data or worry about content-type
        cache: false,
        contentType: false,
        processData: false
    });
    ///end AJAX request
    
    0 讨论(0)
  • 2021-01-01 04:39

    When I implement such a thing, I use

    var fd = new FormData();
    fd.append(file.name,file);
    

    And in the ajax call, send the fd.

    0 讨论(0)
提交回复
热议问题