【发布时间】:2015-05-24 06:45:10
【问题描述】:
我正在使用 HTML5 Drag&Drop File Upload 和 c# asp.net 。 在所有浏览器中一切正常,但在 IE10 中我无法获取服务器中的文件以保存它们。 我已经尝试了一切,但没有任何效果。 请帮忙。
这是我的代码:
javascript 和 HTML:
function uploadFile(index) {
var files = window.opener.files; //get the files from drag area which is in window.opener
for (var i = index; i < (index + files.length); i++) {
debugger;
var size = fixSize(files.item(i - index).size);
var name = files.item(i - index).name;
xhr.push(new XMLHttpRequest()); //create new xhr and push it to xhr array
//var form = window.opener.document.getElementById('ddform');
//var data = new FormData(form);
var data = new FormData();
data.append("index", i);
data.append("fileName", name);
data.append("file", files.item(i - index));
xhr[i].open('POST', "HandlerUploadFiles.ashx", true);
xhr[i].send(data);
}
}
打开窗口中的ddform:
<form id="ddForm" enctype="multipart/form-data" >
<div id="drop_zone">
<asp:Label ID="lblDragDrop" runat="server" Text="Drag File" style="color: #9A9A9A" ClientIDMode="Static"></asp:Label>
</div>
<asp:Button ID='btnRefresh' runat="server" onclick="btnRefresh_Click" style="display:none" />
</form>
c#,使用 IHttpHandler:
public class HandlerUploadFiles : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
// context.Response.ContentType = "text/plain";
string fileName = context.Request["fileName"];
string postedFile = context.Request["file"]; // returns string : {"object file"}
HttpFileCollection fileCollection = context.Request.Files; // always empty!!!
...
}
}
【问题讨论】:
-
我注意到在使用IE10或最新的chrome或ff时,
context.Request.Files有0个键,但是当你查看context.Request时,内容长度在那里。我自己不知道如何从那里获取文件。但在 IE9 及以下和其他较旧的浏览器中,context.Request.Files的长度为 1+ -
你为什么要在自己的 XHR 中发送每个文件?只需将所有文件附加到 for 循环中的
FormData实例,然后只发送一个 XHR。
标签: c# html xmlhttprequest internet-explorer-10 ashx