Reputation: 1844
I'm uploading a file using multipart data form and I need to keep the file description of the uploaded file. I'm using the following code
FileDescription temp = new FileDescription();
var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith<IEnumerable<FileDescription>>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
var fileInfo = streamProvider.FileData.Select(i =>
{
var info = new FileInfo(i.LocalFileName);
temp.AssociatedSchool = 1;
temp.FileName = info.Name;
temp.LocalFileName = i.LocalFileName;
temp.FileSize = info.Length / 1024;
temp.IsFileValid = true;
temp.NoOfRecords = 1;
temp.UploadedBy = 1;
return temp;
});
return fileInfo;
});
This code doesnt set the values to the temp
object. Can anyone tell me an alternate way to get the values? task.Result is always null. How can i get the values out of the thread?
Upvotes: 0
Views: 152
Reputation: 13382
Try change your sample like this
var descriptions = Request.Content.ReadAsMultipartAsync(streamProvider)
.ContinueWith<IEnumerable<FileDescription>>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
var fileInfo = streamProvider.FileData.Select(i =>
{
var info = new FileInfo(i.LocalFileName);
return new FileDescription(){
AssociatedSchool = 1;
FileName = info.Name;
LocalFileName = i.LocalFileName;
FileSize = info.Length / 1024;
IsFileValid = true;
NoOfRecords = 1;
UploadedBy = 1;
}
});
return fileInfo;
}).Result;
var temp = descriptions.First();//Possibly you need FirstOrDefault
Upvotes: 1