ror*_*yok 16 asp.net ajax jquery html5 file-upload
我找到了以下很棒的主题,并解释了如何使用新的HTML5 FormData API通过AJAX/Jquery进行文件上传
这是该代码的略微更新版本,使用较新的JQuery 1.8+语法
$(':button').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: '/Upload', //my ASP.NET MVC method
type: 'POST',
// handle the progress report
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
}
return myXhr;
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
})
.done(function(){
alert("success");
})
.fail(function(){
alert("error");
});
});
function progressHandlingFunction(e){
if(e.lengthComputable){
$('progress').attr({value:e.loaded,max:e.total});
}
}
Run Code Online (Sandbox Code Playgroud)
这是表格
<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="button" value="Upload" />
</form>
<progress></progress>
Run Code Online (Sandbox Code Playgroud)
在服务器端,我们有类似的东西.
[HttpPost]
public string Upload(HttpPostedFileBase file)
{
// do something with file
return "you uploaded a file called " + file.FileName;
}
Run Code Online (Sandbox Code Playgroud)
这非常有效.UNTIL决定在文件对话框中使用"multiple"属性,并发送多个文件.
<form enctype="multipart/form-data">
<input name="file" type="file" multiple="multiple" />
<input type="button" value="Upload" />
</form>
<progress></progress>
Run Code Online (Sandbox Code Playgroud)
您将在网上找到各种页面,建议以下解决方案
public string Upload(IEnumerable<HttpPostedFileBase> files)
{
foreach(var file in files)
...
}
Run Code Online (Sandbox Code Playgroud)
哎呀.不行
public string Upload(List<HttpPostedFileBase> files)
{
foreach(var file in files)
...
}
Run Code Online (Sandbox Code Playgroud)
不.不行.
public string Upload(IEnumerable files)
{
foreach(var file in files)
...
}
Run Code Online (Sandbox Code Playgroud)
甚至不编译
public string Upload(HttpPostedFileBase[] files)
{
foreach(HttpPostedFileBase file in files)
...
}
Run Code Online (Sandbox Code Playgroud)
你猜怎么着?不行.让我们尝试处理Request.Files.好旧的可靠Request.Files.从未失败.
public string Upload()
{
foreach (HttpPostedFileBase uf in Request.Files)
...
}
Run Code Online (Sandbox Code Playgroud)
扰流板警报:它不起作用.
啊哈.得到它了!我将迭代在Request.Files中的键.
public string Upload()
{
foreach(var key in Request.Files.AllKeys)
{
var file = Request.Files[key];
}
}
Run Code Online (Sandbox Code Playgroud)
然而,它不起作用.
ror*_*yok 22
什么做的工作,就是下面,从博客总是可靠的,动态满头里克施特拉尔
public string Upload()
{
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
}
}
Run Code Online (Sandbox Code Playgroud)
这背后的原因是传递给Request.Files
所有文件的文件集合具有相同的名称,因为它们来自单个文件上载对话框.
服务器端方法传递包含文件的单个对象,由于某种原因,Request.Files是获取它的唯一方法.
希望我通过添加这个来节省一些人的头痛.
归档时间: |
|
查看次数: |
10531 次 |
最近记录: |