Reputation: 3826
I am trying to write HTTP content into a FileStream, and I get the error of "Cannot access a close file" at line where I do await CopytoAsync(stream). If I remove the "await", it will continue the operation without any exception, however the written file size is 0KB. Any Idea where I am committing the mistake ?
var provider = new MultipartFormDataStreamProvider(tempdir);
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var content in provider.Contents)
{
using (var stream = new FileStream(serverPath, FileMode.Create, FileAccess.ReadWrite))
{
await content.CopyToAsync(stream);
}
}
Upvotes: 3
Views: 958
Reputation: 3826
I solved the issue, by using the FileData property instead of Content in MultipartFormDataStreamProvider.
Also, I am not using the CopyToAsync anymore, instead I am using the normal File.Move and it works for me perfectly.
var provider = new MultipartFormDataStreamProvider(tempdir);
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var content in provider.FileData)
{
File.Move(content.LocalFileName, serverPath);
}
Upvotes: 4