Created
October 13, 2015 06:50
-
-
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()"
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
#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