In this brief tutorial, we are going to see how to upload Multiple files to use Multipartformdatastreamprovider in asp.net webapi. This concept is based on the multi-section/format data where we can publish the contents of multiple files to not only NameValueCollection as regular form fields provided by the server side. In this tutorial we also see how to override the default behavior of Multipartformdatastreamprovider, storing names in a unique Bodypart_ {GUID} format.
Using System;
Using System.Collections.Generic;
Using System.Diagnostics;
Using System.IO;
Using System.Linq;
Using System.Net;
Using System.Net.Http;
Using System.Net.Http.Headers;
Using System.Threading.Tasks;
Using System.Web;
Using System.Web.Http;
Using SYSTEM.WEB.MVC; Namespace Uploadapplication.controllers {public class Uploadcontroller:apicontroller {public Async Tas
K Test program
Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Net.Http;
Using System.Net.Http.Headers;
Using System.Text;
Using System.Threading.Tasks; Namespace ConsoleApplication1 {class Program {static void Main (string[] args) {usin
G (var client = new HttpClient ()) using (var content = new Multipartformdatacontent ()) { Make sure to change API address client.
baseaddress = new Uri ("http://localhost:53798/"); Add A-content var fileContent1 = new Bytearraycontent (file.readallbytes (@) C:\Users\aisadmin\D
Esktop\me\nf2202533167366.pdf "));
FileContent1.Headers.ContentDisposition = new Contentdispositionheadervalue ("attachment") {
FileName = "Sample.pdf"}; Add Second file content var fileContent2 = new BytearraycOntent (File.readallbytes (@ "C:\Users\aisadmin\Desktop\Sample.txt"));
FileContent2.Headers.ContentDisposition = new Contentdispositionheadervalue ("attachment") {
FileName = "Sample.txt"}; Content.
ADD (FILECONTENT1); Content.
ADD (FileContent2); Make a call to Web API var result = client. Postasync ("/api/upload", content).
result; Console.WriteLine (Result.
StatusCode);
Console.ReadLine ();
}
}
}
}