0

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?

svick
  • 214,528
  • 47
  • 357
  • 477
NewBie
  • 1,714
  • 8
  • 33
  • 65

1 Answers1

1

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
Grundy
  • 13,060
  • 3
  • 33
  • 51