Thursday, November 28, 2019

Asynchronous file uploads in ASP.NET Web API

File upload is quite an important topic for Web API endpoints or for API-driven applications, and sure enough there are some nice changes to the MultiPartFormDataStreamProvider

Let’s have a look at how you could upload files to your ASP.NET Web API.

MultipartFormPost
This method has five parameters. You can increase/decrease the number of parameters according to your requirement. These five parameters are, 
  • Posturl 
    This must be the url to which you want to post the form.
  • userAgentThis is up to your requirement; if needed, then pass the value as required.
  • postParametersThis is of type Dictionary. You can pass the parameter name and value as “key-value”
  • headerkeyThis must be the name of the header that needs to be passed. In this example, I have used it as a string which can be used to pass a single header. If the header is not required, you can ignore this parameter.
  • headervalueThis must be the value of the header to be passed.

Let’s modify our controller now

Returning some meaningful info

Finally, you may want to return some information about the uploaded files to the client. If that’s the case, one way to do it is to use a helper class (which was used already in the older post):

2
3
4
5
6
7
8
9
10
11
12
13
public class FileDesc
{
public string Name { get; set; }
public string Path { get; set; }
public long Size { get; set; }
 
public FileDesc(string n, string p, long s)
{
            Name = n;
            Path = p;
            Size = s;
}
}

However, now, you can easily derive from the default MultiPartFormDataStreamProvider and provide your own naming mechanism.

Let’s have a look at such simple example:

public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public CustomMultipartFormDataStreamProvider(string path) : base(path)
{}
 
        public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
        {
            var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName";
            return name.Replace(""",string.Empty); //this is here because Chrome submits files in quotation marks which get treated as part of the filename and get escaped
        }
}


So now, instead of void we return a List<FileDesc> which can provide the client information about each of the uploaded files: its name, path and size.




Kafka setup in window

Here's a step-by-step guide on how to do it : 1. Prerequisites : Before you begin, make sure you have the following prerequisites inst...