0

我在我的网站(asp.net Web 应用程序)中使用 HTML5 拖放上传。上传过程需要在每个文件的带有进度条的弹出窗口中执行(因此包含拖动区域的页面将保持异步运行)。我需要从服务器(C#)访问文件,而不仅仅是从 javascript。问题是 context.Request.Files 在弹出窗口中是空的。当我尝试在同一页面中编写所有代码时,Request.Files 已满。

这是我的代码:

从“父”页面(jquery)打开弹出窗口:

 function handleDnDFileSelect(event) {
        event.stopPropagation();
        event.preventDefault();
        /* Read the list of all the selected files. */
        files = event.dataTransfer.files;
        var fileCount = files.length;
        // Only call the handler if 1 or more files was dropped.
        if (fileCount > 0) {
            window.open("UploadProgress.aspx", "progress", 'menubar=1,resizable=1,width=750,height=450');
        } 
    }

上传进度.aspx:

<script>
$(document).ready(function () {
    debugger;
    var files = window.opener.files; //files is full corectly
    var form = document.getElementById('form1');
    var data = new FormData(form);
    for (var i = 0; i < files.length; i++) //creating a new XMLHttpRequest for each file
    {
        data.append(files[i].name, files[i]);
        var xhr = new XMLHttpRequest();
        xhr.open('POST', "UploadProgress.aspx");
        xhr.send(data);
    }
});
</script>
<body>
<form id="form1" runat="server" data-ajax="false" enctype="multipart/form-data">
<asp:Repeater ID="rptFilesUpload" runat="server">
 <ItemTemplate>
    <div id="progressbar">
        <asp:Label ID="lblFileName" runat="server" Text='<%# Container.DataItem %>' ></asp:Label>
    </div>
 </ItemTemplate>
</asp:Repeater>
</form></body>

上传进度.aspx.cs:

public partial class UploadProgress : System.Web.UI.Page, IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "multipart/form-data";
        HttpFileCollection fileCollection = context.Request.Files;// Session["Files"] as HttpFileCollection;
        ArrayList FilesNames = new ArrayList();
        for (int i = 0; i < fileCollection.Count; i++)
        {
            HttpPostedFile upload = fileCollection[i];
            string filename = "c:\\Test\\" + upload.FileName;
            upload.SaveAs(filename);
            //update arrayList for the repeater
            FilesNames.Add(upload.FileName);
        }
        rptFilesUpload.DataSource = FilesNames;
        rptFilesUpload.DataBind();
    }

我究竟做错了什么?我正在寻找几天的解决方案............我将不胜感激任何尝试解决我的问题。

4

1 回答 1

0

我仍然不知道为什么它不起作用。但是,我只是将 ProcessRequest 函数放在 ashx (处理程序)中并更改 xhr.open('POST', "UploadProgress.aspx"); 到 xhr.open('POST', "UploadProgressHandler.ashx"); 它奏效了!(仅在 FireFox 和 Chrome 中。在 IE(10+)中我仍在寻找解决方案)

于 2013-11-28T09:00:00.653 回答