Created
February 27, 2019 23:18
-
-
Save kparkov/3fd6565f9af870139683fd0bf4b97a79 to your computer and use it in GitHub Desktop.
Upload stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost, Route("project/{projectId}/folder/{folderId}/upload")] | |
public async Task<object> UploadFile(string projectId, string folderId) | |
{ | |
var request = Request; | |
// This solution was provided by: http://www.asp.net/web-api/overview/advanced/sending-html-form-data-part-2 | |
// 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 | |
{ | |
// Read the form data. | |
await Request.Content.ReadAsMultipartAsync(provider); | |
// This illustrates how to get the file names. | |
foreach (MultipartFileData file in provider.FileData) | |
{ | |
var realName = file.Headers.ContentDisposition.FileName.Replace("\"", ""); | |
var tempUploadName = file.LocalFileName; | |
await Projects.HandleStagedFile(projectId, folderId, tempUploadName, realName); | |
} | |
return Request.CreateResponse(HttpStatusCode.OK); | |
} | |
catch (Exception e) | |
{ | |
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); | |
} | |
return new {status = "OK"}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment