Skip to content

Instantly share code, notes, and snippets.

@kparkov
Created February 27, 2019 23:18
Show Gist options
  • Save kparkov/3fd6565f9af870139683fd0bf4b97a79 to your computer and use it in GitHub Desktop.
Save kparkov/3fd6565f9af870139683fd0bf4b97a79 to your computer and use it in GitHub Desktop.
Upload stream
[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