2012-05-12 5 views
0

ASP.NET MVC3에서 개발 중이며 SQL Server 2008에서 파일을 저장하기위한 코드가 있습니다. IE (IE9)를 사용하면 잘 작동하지만 파이어 폭스에서 "인덱스가 범위를 벗어났습니다. 음수가 아니고 콜렉션의 크기보다 작아야합니다. \ r \ n 매개 변수 이름 : 인덱스"라는 오류 메시지가 나타납니다. 어떻게 수정해야합니까? 감사합니다Valums Ajax 파일 업로드는 IE에서 작동하지만 Firefox에서는 작동하지 않습니다.

[HttpPost] 
    public ActionResult FileUpload(string qqfile) 
    { 
     try 
     { 
      HttpPostedFileBase postedFile = Request.Files[0]; 
      var stream = postedFile.InputStream; 
      App_MessageAttachment NewAttachment = new App_MessageAttachment 
      { 
       FileName = postedFile.FileName.ToString().Substring(postedFile.FileName.ToString().LastIndexOf('\\') + 1), 
       FilteContentType = postedFile.ContentType, 
       MessageId = 4, 
       FileData = new byte[postedFile.ContentLength] 
      }; 
      postedFile.InputStream.Read(NewAttachment.FileData, 0, postedFile.ContentLength); 
      db.App_MessageAttachments.InsertOnSubmit(NewAttachment); 
      db.SubmitChanges(); 
     } 
     catch (Exception ex) 
     { 
      return Json(new { success = false, message = ex.Message }, "application/json"); 
     } 
     return Json(new { success = true }, "text/html"); 
    } 

답변

2

Valums Ajax 업로드에는 2 가지 모드가 있습니다. 브라우저가 HTML5 File API (의심 할 여지없이 FireFox의 경우)를 지원한다는 것을 인식하면 enctype="multipart/form-data" 요청 대신이 API를 사용합니다. 당신이 그 차이 및 지원 최신 브라우저의 경우 고려해야 컨트롤러의 행동 그래서 HTML5 직접 Request.InputStream을 읽어

[HttpPost] 
public ActionResult FileUpload(string qqfile) 
{ 
    try 
    { 
     var stream = Request.InputStream; 
     var filename = Path.GetFileName(qqfile); 

     // TODO: not sure about the content type. Check 
     // with the documentation how is the content type 
     // for the file transmitted in the case of HTML5 File API 
     var contentType = Request.ContentType; 
     if (string.IsNullOrEmpty(qqfile)) 
     { 
      // IE 
      var postedFile = Request.Files[0]; 
      stream = postedFile.InputStream; 
      filename = Path.GetFileName(postedFile.FileName); 
      contentType = postedFile.ContentType; 
     } 
     var contentLength = stream.Length; 

     var newAttachment = new App_MessageAttachment 
     { 
      FileName = filename, 
      FilteContentType = contentType, 
      MessageId = 4, 
      FileData = new byte[contentLength] 
     }; 
     stream.Read(newAttachment.FileData, 0, contentLength); 
     db.App_MessageAttachments.InsertOnSubmit(newAttachment); 
     db.SubmitChanges(); 
    } 
    catch (Exception ex) 
    { 
     return Json(new { success = false, message = ex.Message }); 
    } 
    return Json(new { success = true }, "text/html"); 
} 

코드는 일부 조정이 필요할 수 있습니다. 지금 당장은 테스트 할 시간이 없지만 아이디어를 얻습니다. HTML5 지원 브라우저의 경우 파일은 요청 본문에 직접 쓰여지므로 File API를 지원하지 않는 브라우저의 경우 파일 데이터가 전송됩니다 표준 multipart/form-data 인코딩을 사용합니다.

관련 문제