2013-11-28 6 views
1

HTML5 드래그를 사용하고 있습니다. & C# asp.net을 사용하여 파일 업로드를 삭제하십시오. 모든 브라우저에서 모든 것이 정상적으로 작동하지만 IE10에서는 파일을 저장하기 위해 서버에서 파일을 가져올 수 없습니다. 나는 모든 것을 시도했지만 아무 것도 작동하지 않습니다. 도와주세요. & HTMLRequest.Files는 xmlHttpRequst 및 ashx를 사용하는 ie10에서만 비어 있습니다.

자바 스크립트 : 오프너 창에서

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로 각 파일을 보냅니 까? 모든 파일을 for 루프의'FormData' 인스턴스에 추가 한 다음 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); 
    } 
}