Skip to content

Instantly share code, notes, and snippets.

@gpapadopoulos
Created October 13, 2015 06:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpapadopoulos/09a781d5f2cbca4112f6 to your computer and use it in GitHub Desktop.
Save gpapadopoulos/09a781d5f2cbca4112f6 to your computer and use it in GitHub Desktop.
How to get the Stream of a file from a Request object using "Request.Content.ReadAsStreamAsync()"
#region TEST "Request.Content.ReadAsStreamAsync()"
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var item in provider.Contents.Where(item => item.Headers.ContentDisposition.FileName != null))
{
Stream streamRead = await item.ReadAsStreamAsync();
// Read the next chunk from file and upload it (no verify)
long offset = 0;
FileStream streamWrite = File.Create(@"C:\SERVER_FILES\" + item.Headers.ContentDisposition.FileName.Replace("\"", string.Empty));
while (streamRead.Position < streamRead.Length) // TODO: COULD USE IT FOR THE FILE SIZE
{
var nextChunkSize = Math.Min(1024, (int) (streamRead.Length - offset));
var data = new byte[nextChunkSize];
streamRead.Read(data, 0, nextChunkSize);
streamWrite.Write(data, 0, nextChunkSize);
offset += nextChunkSize;
}
streamRead.Close();
streamWrite.Close();
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment