Isaac Levin
Isaac Levin

Reputation: 2899

MultipartMemoryStream in Dot Net Core

I have a .Net 4.5.2 App that I am moving to Dot Net Core. This app allows the user to upload a file with Metadata (Angular Client Side) and the Api will handle the request and process the file. Here is the existing code that does that.

Api

[HttpPost]
[Route("AskQuestions")]
public void ProvideClarifications(int id)
{
  var user = base.GetUserLookup();
  if (user != null)
  {
    var streamProvider = new MultiPartStreamProvider();
    IEnumerable<HttpContent> parts = null;
    Task.Factory
        .StartNew(() => parts = Request.Content.ReadAsMultipartAsync(streamProvider).Result.Contents,
            CancellationToken.None,
            TaskCreationOptions.LongRunning,
            TaskScheduler.Default)
        .Wait();

        // do some stuff with streamProvider.FormData
  }
}

Provider to Handle File and MetaData

public class MultiPartStreamProvider : MultipartMemoryStreamProvider
{
    private string _originalFileName = string.Empty;
    public Dictionary<string, object> FormData { get; set; }

    public byte[] ByteStream { get; set; }

    public string FileName
    {
        get
        {
            return _originalFileName.Replace("\"", "");
        }
    }
    public MultiPartStreamProvider()
    {
        this.FormData = new Dictionary<string, object>();
    }
    public override Task ExecutePostProcessingAsync()
    {
        foreach (var content in Contents)
        {
            var contentDispo = content.Headers.ContentDisposition;
            var name = UnquoteToken(contentDispo.Name);

            if (name.Contains("file"))
            {
                _originalFileName = UnquoteToken(contentDispo.FileName);
                this.ByteStream = content.ReadAsByteArrayAsync().Result;
            }
            else
            {
                var val = content.ReadAsStringAsync().Result;
                this.FormData.Add(name, val);
            }
        }
        return base.ExecutePostProcessingAsync();
    }
    private static string UnquoteToken(string token)
    {
        if (String.IsNullOrWhiteSpace(token))
        {
            return token;
        }

        if (token.StartsWith("\"", StringComparison.Ordinal) && token.EndsWith("\"", StringComparison.Ordinal) && token.Length > 1)
        {
            return token.Substring(1, token.Length - 2);
        }

        return token;
    }
}
static class FormDataExtensions {
    public static Object GetObject(this Dictionary<string, object> dict, Type type)
    {
        var obj = Activator.CreateInstance(type);

        foreach (var kv in dict)
        {
            var prop = type.GetProperty(kv.Key);
            if (prop == null) continue;

            object value = kv.Value;
            var targetType = IsNullableType(prop.PropertyType) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType;

            if (value is Dictionary<string, object>)
            {
                value = GetObject((Dictionary<string, object>)value, prop.PropertyType); // <= This line                   
            }
            value = Convert.ChangeType(value, targetType);
            prop.SetValue(obj, value, null);
        }
        return obj;
    }
    public static T GetObject<T>(this Dictionary<string, object> dict)
    {
        return (T)GetObject(dict, typeof(T));
    }
    private static bool IsNullableType(Type type)
    {
        return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
    }
}

So this works totally fine targeting framework, but in Core I get an exception on this line

.StartNew(() => parts = Request.Content.ReadAsMultipartAsync(streamProvider).Result.Contents,

Exception

'HttpRequest' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?)

What do I need to make sure I can get this file and metadata? Is there a way in Core of getting HttpContent from a HttpRequest

Upvotes: 5

Views: 6283

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100701

Asp.Net Core has support for multipart file uploads built-in. The model binding component will make this available when you have a List<IFormFile> parameter.

See the docs on file uploads for more details, here is the relevant example it gives for handling multipart uploads:

[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(List<IFormFile> files)
{
    long size = files.Sum(f => f.Length);

    // full path to file in temp location
    var filePath = Path.GetTempFileName();

    foreach (var formFile in files)
    {
        if (formFile.Length > 0)
        {
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await formFile.CopyToAsync(stream);
            }
        }
    }

    // process uploaded files
    // Don't rely on or trust the FileName property without validation.

    return Ok(new { count = files.Count, size, filePath});
}

Upvotes: 6

Related Questions