2017-03-08 3 views
-1

multipart/form-data을 사용하여 파일을 업로드 할 수는 있지만 multipart/form-data 폴더를 업로드하는 방법에 대한 자습서를 찾을 수 없습니다. 이건 내 코드는 파일을 업로드한다 :다중 파트/양식 데이터를 사용하여 폴더를 업로드 할 수 있습니까?

HTML :

<form name="form1" method="post" enctype="multipart/form-data" action="api/upload"> 
      <fieldset> 
       <legend>File Upload Example</legend> 
      <div> 
       <label for="caption">Image Caption</label> 
       <input name="caption" type="text" /> 
      </div> 
      <div> 
       <label for="image1">Image File</label> 
       <input name="image1" type="file" /> 
      </div> 
      <div> 
       <input type="submit" value="Submit" /> 
      </div> 
       </fieldset> 
     </form> 

컨트롤러 :

public class UploadController : ApiController 
{ 
    [AcceptVerbs("GET", "POST")] 
    public async Task<HttpResponseMessage> PostFile() 
    { 
     // Check if the request contains multipart/form-data. 
     if (!Request.Content.IsMimeMultipartContent()) 
     { 
      throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 
     } 

     string root = HttpContext.Current.Server.MapPath("~/App_Data"); 
     var provider = new MultipartFormDataStreamProvider(root); 

     try 
     { 
      StringBuilder sb = new StringBuilder(); // Holds the response body 

      // Read the form data and return an async task. 
      await Request.Content.ReadAsMultipartAsync(provider); 


      // This illustrates how to get the file names for uploaded files. 
      foreach (var file in provider.FileData) 
      { 
       var originalFile = file.Headers.ContentDisposition.FileName.TrimStart('"').TrimEnd('"'); ; 
       FileInfo fileInfo = new FileInfo(file.LocalFileName); 
       fileInfo.CopyTo(Path.Combine(root, originalFile), true); 

       sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", originalFile, fileInfo.Length)); 
       fileInfo.Delete(); 
      } 

      return new HttpResponseMessage() 
      { 
       Content = new StringContent(sb.ToString()) 
      }; 
     } 
     catch (System.Exception e) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); 
     } 
    } 

} 

내가 폴더를 업로드 할 multipart/form-data를 사용할 수 있습니까?

+0

파일 업로드는 애매한 개념 인 '폴더'에 대해 언급하지 않은 RFC 1867 (https://www.ietf.org/rfc/rfc1867.txt)을 기반으로합니다. 그래서 당신은 파일 목록을 업로드 할 수 있습니다. 그게 전부예요. –

+0

javascript로 각 파일을 업로드하기 전에 폴더의 목록 파일을 어떻게 얻을 수 있습니까? –

+2

https://developer.mozilla.org/en/docs/Using_files_from_web_applications 여기에 설명되어 있습니다 :''을 사용하거나'event.datatransfer.files'와 함께 드롭 존을 사용하십시오 https://developer.mozilla.org/en/docs/Web/API/DataTransfer/files –

답변

0

질문이 "거기는 하나의 폴더를 선택하고 브라우저가있는 모든 파일을 업로드해야하는 브라우저에서 사용자의 방법이다"는, 그 대답은 브라우저에 따라 다릅니다.

표준에는 폴더 내용을 forminput type=file 컨트롤로 업로드하는 방법이 나와 있지 않습니다. 브라우저는 지원 범위가 다양합니다.

Google Chrome, which supports folder upload as per this question.

Firefox may be able to do something via drag-n-drop

MS Edge browser support is "in development"

이 브라우저는 스크립트가 사용자 상호 작용없이 로컬 파일에 액세스 할 수 없습니다.

사용자가 모든 파일을 브라우저로 드래그하거나 다른 프로그램을 사용하여 파일을 업로드하려는 경우에는 가능하지만 브라우저를 사용하여 서버에 폴더를 보내면 사양을 벗어납니다.

(서버 측에서는 경로가 지정된 여러 파일을 허용하도록 선택할 수 있지만 브라우저는이 파일을 사용자에게 보내지 않으므로 사용자 쪽에서 특별히 작성된 프로그램을 보내야합니다. 파일)

+0

이 샘플 : ajaxuploader.com/demo/upload-folders.aspx, 우리는 폴더를 업로드 할 수 있습니다. –

+0

@DT 좋아요, Chrome 관련 추가 사항입니다. [Chrome의 폴더 업로드 사용에 관한이 질문] (/ questions/5826286 /)을 확인하십시오. – Stobor

0

불행히도 사양에없는 폴더는 업로드 할 수 없습니다. 하지만이 코드는 시도 할 수 없습니다.

클라이언트 측 이미지 캡션에 관한

<form name="form1" method="post" enctype="multipart/form-data" action="api/upload"> 
     ... 
     <div> 
      <label for="folderName">Folder Name</label> 
      <input name="folderName" type="text" /> 
     </div> 
     <div> 
      <label for="images">Images</label> 
      <!-- you choose multiple files --> 
      <input name="images" type="file" multiple /> 
     </div> 
     ... 
    </form> 

서버 측

// using ASP Logic 
// create folder in your server 
// put uploaded files to the created folder 
// save files info and captions to DB 

, 보라하시기 바랍니다. Html/PHP - Form - Input as array

+0

이 샘플 : http://ajaxuploader.com/demo/upload-folders.aspx, 우리는 폴더를 업로드 할 수 있습니다. –