2013-11-10 32 views
0

我使用HTML5拖放上傳在我的網站(asp.net web應用程序)。 需要在每個文件的進度條彈出窗口中執行上傳過程(因此包含拖動區域的頁面將保持異步運行)。 我需要從服務器(C#)訪問文件,而不僅僅是從JavaScript。 問題是在彈出窗口中context.Request.Files是空的。當我嘗試在同一頁面中寫入所有代碼時Request.Files已滿。Html5拖放,用彈出窗口上傳下載的文件到服務器(asp.net)

這裏是我的代碼:

從 「父」 頁面(jQuery的)打開popupwindoe:

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'); 
     } 
    } 

uploadProgress.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(); 
    } 

我在做什麼錯? 我正在尋找天的解決方案......... 我會感謝任何嘗試解決我的問題。

回答

0

我仍然不知道爲什麼它沒有工作。 但是,我只是把ProcessRequest函數放在ashx(handler) 並改變了xhr.open('POST',「UploadProgress.aspx」); to xhr.open('POST',「UploadProgressHandler.ashx」); 它工作! (僅在FireFox和Chrome中,在IE(10+)中我仍在尋找解決方案)