Custom Media formatter – HttpClient

send file from one solution to other solution

public void UploadDomainAssembly(string filename)
String strFile = System.IO.Path.GetFileName(filename);

using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
// Make sure to change API address
client.BaseAddress = new Uri(“http://localhost:8090/”);

var fileContent = new ByteArrayContent(File.ReadAllBytes(filename));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue(“AssemblyAttachment”)
FileName = strFile


// Make a call to Web API
var result = client.PostAsync(“/api/Values/UploadDomainAssembly”, content).Result;

//dynamic isAssembly = await this.For<Validation>().Function(“UploadDomainAssembly”).Post<dynamic>(new { content });
//return isAssembly;

//dynamic isAssembly = await this.For<Validation>().Function(“UploadDomainAssembly”).Post<dynamic>(new { filename });
//return isAssembly;
return null;

public async Task<dynamic> UploadFileAsync(Stream inputStream, string fileName)
RestSharp.RestRequest restRequest = new RestSharp.RestRequest();
restRequest.AddFile(“fileData”, inputStream.CopyTo, fileName);
restRequest.AlwaysMultipartFormData = true;
restRequest.AddParameter(“key”, “value”, RestSharp.ParameterType.GetOrPost);

var response = await this.Client.ExecutePostTaskAsync(restRequest);

var result = JsonConvert.DeserializeObject<dynamic>(response.Content);
return result;

Web API Service




public async Task<HttpResponseMessage> UploadDomainAssembly()


if (!Request.Content.IsMimeMultipartContent())


throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);



string fileSaveLocation = HttpContext.Current.Server.MapPath(“~/App_Data”);

CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);

List<string> files = new List<string>();




// Read all contents of multipart message into CustomMultipartFormDataStreamProvider.

await Request.Content.ReadAsMultipartAsync(provider);


foreach (MultipartFileData file in provider.FileData)





// Send OK Response along with saved file names to the client.

return Request.CreateResponse(HttpStatusCode.OK, files);


catch (System.Exception e)


return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);





public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider


public CustomMultipartFormDataStreamProvider(string path) : base(path) { }


public override string GetLocalFileName(HttpContentHeaders headers)


return headers.ContentDisposition.FileName.Replace(“\””, string.Empty);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s