【问题标题】:File Upload with jQuery AJAX and Handler (ashx) not working使用 jQuery AJAX 和处理程序 (ashx) 上传文件不起作用
【发布时间】:2013-06-02 13:28:42
【问题描述】:

我正在尝试使用 jQuery AJAX 和通用处理程序上传图像文件。但似乎该文件没有被传递给处理程序。 提交后context.Request.Files[0]; 始终为空:-/

我做错了什么?

HTML:

<form id="form1" runat="server" method="post" enctype="multipart/form-data">

    <input name="file" id="file" type="file" />
    <input id="save" name="submit" value="Submit" type="submit" />

</form>

JS:

$().ready(function ()
{
    $('#file').change(function () 
    {
        sendFile(this.files[0]);
    });
});

function sendFile(file) 
{
    $.ajax({
        type: 'post',
        url: 'FileUpload.ashx',
        data: file,
        success: function () {
            // do something
        },
        xhrFields:
        {
            onprogress: function (progress) 
            {
                // calculate upload progress
                var percentage = Math.floor((progress.total / progress.totalSize) * 100);

                // log upload progress to console
                console.log('progress', percentage);

                if (percentage === 100) {
                    console.log('DONE!');
                }
            }
        },
        processData: false,
        contentType: 'multipart/form-data'
    });
}

ASHX:

public void ProcessRequest (HttpContext context) 
{
    HttpPostedFile file = context.Request.Files[0];

    if (file.ContentLength > 0)
    {
        //do something
    }
}

【问题讨论】:

  • 您的代码中某处有一个 jquery 引用,对吧?
  • 是的,我有。正在调用处理程序,但没有加载文件:-/

标签: c# jquery asp.net ajax generic-handler


【解决方案1】:

设法让这个工作:)

这是我的代码...

///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

【讨论】:

  • 感谢您发布此内容...它对我来说太棒了
  • 完美,我在网上找到的其他“解决方案”似乎对我不起作用,但确实如此。
【解决方案2】:

当我实现这样的事情时,我使用

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

在 ajax 调用中,发送fd

【讨论】:

  • 进行该更改后未调用处理程序,可能是什么问题?
  • 可能是客户端错误。如果你有一个活生生的例子,那就太好了。如果没有,并且您使用的是 chrome/firefox,请检查调试器控制台并告诉我您在那里看到的内容。
  • 在控制台中我收到以下错误:Uncaught SyntaxError: Not enough arguments我已经上传了演示,所以你可以试一试:online demo
  • 我更正了我的答案 - 我错误地省略了第一个参数,即文件名。
  • 现在我收到了这个错误:POST http://localhost:10081/FileUpload.ashx 500 (Internal Server Error) x.support.cors.e.crossDomain.send x.extend.ajax sendFile (anonymous function) x.event.dispatch y.handle
猜你喜欢
  • 2012-01-18
  • 2017-03-08
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
相关资源
最近更新 更多