2013-11-28 2 views
1

Я использую HTML5 Перетащите & Загрузите файл с помощью C# asp.net. Во всех браузерах все работает нормально, но в IE10 я не могу получить файлы на сервере, чтобы их сохранить. Я пробовал все, и ничего не работает. прошу помочь.Request.Files пуст только в ie10 с помощью xmlHttpRequst и ashx

вот мой код:

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!!! 
     ... 
    } 
} 
+0

Я заметил, что при использовании IE10 или последнего хрома или ff в файле 'context.Request.Files' есть 0 ключей, но когда вы смотрите в' context.Request', длина содержимого есть. Я сам не знаю, как получить файл (ы) оттуда. Но в IE9 и ниже и других старых браузерах 'context.Request.Files' имеет длину 1+ – Pierre

+0

Почему вы отправляете каждый файл в свой собственный XHR? Просто добавьте все файлы к экземпляру 'FormData' в for-loop, а затем отправьте только один XHR. – idbehold

ответ

0
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); 
    } 
} 
Смежные вопросы