2015-03-13 20 views
2
<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和下面是我处理程序文件的代码......我已经正确写入了我的jquery代码和处理程序文件代码。但值不传递到处理程序文件从jQuery的文件

,直到结束我得到的价值,同时调试,但在在同时使上传多张图片的座右铭,我无法在处理任何价值

处理程序代码

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"); 
     } 
    } 
+0

是否包含在你的''document.ready' '如果在所有它被放置在HTML – 2015-03-13 09:26:23

+0

是的,我这样做..... – 2015-03-13 09:53:11

回答

1

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

$(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是不是在旧的浏览器可用,您的代码将静默失败。如果您需要支持旧的浏览器,你可能需要通过测试浏览器的功能,并回落到一个标准的表单POST进行渐进增强,如果浏览器不支持FormData

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谁来寻找答案。

+0

不,不。 ....仍然是不可行的....通过调试我才知道我的代码运行不错,但只有问题是在处理程序没有文件被请求后 – 2015-03-13 12:11:58

+0

肥胖你是否提到我在末尾提到的链接? – 2015-03-13 13:00:06

+0

nop nop .....我需要问一个东西......在你的链接代码中你的代码的控制器部分.....我应该在哪里写这个.....在哪个文件中.. ..? – 2015-03-16 05:45:49

1

使用HttpContextBase[],而不是仅仅HttpContext

相关问题