locked
Cannot convert from system.threading.tasks.task <byte[]> to byte[] RRS feed

  • Question

  • User931778073 posted

    I am following a tutorial on uploading files using Web API 2 from an article on the website https://chris.59north.com/post/Uploading-files-using-ASPNET-Web-Api  . I followed every thing as shown in the article but I'm getting the error "Cannot convert from system.threading.tasks.task <string> to string" on the line

    "files.Add(fieldName, new HttpPostedFile(fieldName, fileName, file));"

    Please look at my code below and help point out why I'm getting this error.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace CanvasModifyier.Extensions
    {
        public static class HttpContentExtensions
        {
            public static async Task<HttpPostedData> ParseMultipartAsync(this HttpContent postedContent) {
    
                //Get Content
                var provider = await postedContent.ReadAsMultipartAsync();
    
                //Create two dictionaries
                var files = new Dictionary<string, HttpPostedFile>(StringComparer.InvariantCultureIgnoreCase);
                var fields = new Dictionary<string, HttpPostedField>(StringComparer.InvariantCultureIgnoreCase);
    
                //Sort Content into files and fields
                foreach (var content in provider.Contents)
                {
                    var fieldName = content.Headers.ContentDisposition.Name.Trim('"');
    
                    //Has a Filename, so it must be a file
                    if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName)) {
                        var file = content.ReadAsByteArrayAsync();
                        var fileName = content.Headers.ContentDisposition.FileName.Trim('"');
                        files.Add(fieldName, new HttpPostedFile(fieldName, fileName, file));
                    }
                    else
                    {
                        var data = content.ReadAsStringAsync();
                        
                        fields.Add(fieldName, new HttpPostedField(fieldName, data));
                    }                
                }
    return new HttpPostedData(fields, files); } } public class HttpPostedData { public HttpPostedData(IDictionary<string, HttpPostedField> fields, IDictionary<string, HttpPostedFile> files) { Fields = fields; Files = files; } public IDictionary<string, HttpPostedField> Fields { get; private set; } public IDictionary<string, HttpPostedFile> Files { get; private set; } } public class HttpPostedField { public HttpPostedField(string name, string value) { Name = name; Value = value; } public string Name { get; private set; } public string Value { get; private set; } } public class HttpPostedFile { public HttpPostedFile(string name, string filename, byte[] file) { Name = name; Filename = filename; File = file; } public string Name { get; private set; } public string Filename { get; private set; } public byte[] File { get; private set; } } }

    Monday, December 30, 2019 5:16 AM

Answers

  • User303363814 posted

    You've missed the 'await'

    var file = await content.ReadAsByteArrayAsync();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 30, 2019 11:14 PM

All replies

  • User303363814 posted

    You've missed the 'await'

    var file = await content.ReadAsByteArrayAsync();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 30, 2019 11:14 PM
  • User931778073 posted

    Thanks for your help, that was one of the causes of the error. The other was I was missing the await keyword in the following line as well:

    var data = content.ReadAsStringAsync();
    Tuesday, December 31, 2019 2:15 AM