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
를 사용할 수 있습니까?
파일 업로드는 애매한 개념 인 '폴더'에 대해 언급하지 않은 RFC 1867 (https://www.ietf.org/rfc/rfc1867.txt)을 기반으로합니다. 그래서 당신은 파일 목록을 업로드 할 수 있습니다. 그게 전부예요. –
javascript로 각 파일을 업로드하기 전에 폴더의 목록 파일을 어떻게 얻을 수 있습니까? –
https://developer.mozilla.org/en/docs/Using_files_from_web_applications 여기에 설명되어 있습니다 :''을 사용하거나'event.datatransfer.files'와 함께 드롭 존을 사용하십시오 https://developer.mozilla.org/en/docs/Web/API/DataTransfer/files –