【问题标题】:Request.Files is empty only in ie10 using xmlHttpRequst and ashxRequest.Files 仅在使用 xmlHttpRequst 和 ashx 的 ie10 中为空
【发布时间】: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


【解决方案1】:
string _fileName = string.Empty;
public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    try
    {
        if (context.Request.Files.Count > 0)
        {
            HttpFileCollection files = context.Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile file = files[i];
                if (file.ContentLength / 1024 < 1024 * 1) //1024*1 = 1 MB
                {
                    _fileName = file.FileName;
                    string fname =
                        context.Server.MapPath("~/tools/DragAndDropFileUpload/uploads/" +
                                               MyRandomChar.StringIntNumber(20) + Path.GetExtension(_fileName));
                    file.SaveAs(fname);
                }
                else
                    throw new Exception("Maximum file size exceeded.");

            }
            context.Response.Write("Uploaded Successfully!");
        }
    }
    catch (Exception ex)
    {
        context.Response.Write(ex.Message);
    }
}

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2021-05-20
    • 2012-02-13
    • 2013-04-04
    • 2015-04-12
    • 2012-12-20
    相关资源
    最近更新 更多