javascript - 文件直到处理程序的方法才到达

标签 javascript jquery file-upload c#-3.0 handler

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
$(document).ready(function () {
            $("#Button1").click(function (evt) {
                var fileUpload = $('[id$=FileUpload1]')[0].value.split(",");

                      var data = new FormData();
                     for (var i = 0; i < fileUpload.length; i++) {
                          data.append(fileUpload[i].name, fileUpload[i]);
                        }

    var options = {};
    options.url = "Handler.ashx";
    options.type = "POST";
    options.data = data;
    options.contentType = false;
    options.processData = false;
    options.success = function (result) { alert(result); };
    options.error = function (err) { alert(err.statusText); };

   $.ajax(options);

   evt.preventDefault();
  });
});

这是我的 jquery,下面是我的处理程序文件代码......

直到最后,我在调试时获得了值(value),但在一次上传多张图片的座右铭中,我无法在句柄中获得任何值(value)

处理程序代码

public void ProcessRequest (HttpContext context) {

        string filePath = "FileSave//";

        foreach (string file in context.Request.Files)
        {
            HttpPostedFile filed = context.Request.Files[file];
            filed.SaveAs(context.Server.MapPath(filePath  + filed.FileName));
            context.Response.Write("File uploaded");
        }
  }

最佳答案

如果你愿意,你可以尝试这种方式。

$(document).ready(function () {
     $("#Button1").click(function (evt) {
          evt.preventDefault();
          var formdata = new FormData();
          var fileInput = $('#sliderFile'); //#sliderFile is the id of your file upload control
          if ($(fileInput).get(0).files.length == 0)
          { //show error
               return false;
          }
          else
          {
              $.each($(fileInput).get(0).files, function (index,value) {
                   formdata.append(value.name, value);
              });
              $.ajax({
                    url: 'Handler.ashx',
                    type: "POST",
                    dataType: 'json',
                    data: data,
                    processData: false,
                    contentType:false,
                    success: function (data) {
                            if (data.result) {
                                 //return true or any thing you want to do here
                            }
                            else {
                                 //return false and display error
                            }
                    },
                    error: function (data) {
                          //return false and display error
                    }
              });
          }
     });//button click close
 });//document.ready close

尝试一下然后告诉我

编辑:请记住,HTML5 FormData 在旧版浏览器中不可用,您的代码将默默地失败。如果您需要支持较旧的浏览器,您可能需要通过测试浏览器的功能来执行渐进式增强,如果浏览器不支持 FormData,则回退到标准表单 POST:

if(window.FormData === undefined) {
        // The browser doesn't support uploading files with AJAX
        // falling back to standard form upload
} else {
        // The browser supports uploading files with AJAX =>
        // we prevent the default form POST and use AJAX instead
        e.preventDefault();

        ...
}

有关这方面的更多信息,您可以查看我已接受的其中一个问题的答案。很清楚那里的问题是什么。 Here is the link

编辑:只需添加这些 LINK1LINK2对于那些前来寻找答案的人。

关于javascript - 文件直到处理程序的方法才到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29028592/

相关文章:

javascript - 使用 QUnit 测试 Dojos JsonRest

javascript - react 渲染对象数组

javascript - 如何从列表中编号值的开头和结尾删除双引号

php - Zend\File\Transfer\Adapter\Http on receive : error "File was not found" with jQuery File Upload

javascript - Redux - 如何从商店获取数据并发布它

javascript - jScrollPane 未显示在 div #container 中

jquery - 在元素下方显示 jquery 验证错误

vue.js - 如何删除上传到 vuetify 的 3 个文件中的 1 个文件?

javascript - 如何将id作为FileUpload的参数传递给javascript函数以检查文件的扩展名和大小

jquery - Ajax/Jquery & Laravel - 动态下拉选择并将选择值提取到文本框