Here is an example of saving a file on a server. Hope this helps you.
public class TestController : ApiController { const string StoragePath = @"T:\WebApiTest"; public async Task<HttpResponseMessage> Post() { if (Request.Content.IsMimeMultipartContent()) { var streamProvider = new MultipartFormDataStreamProvider(Path.Combine(StoragePath, "Upload")); await Request.Content.ReadAsMultipartAsync(streamProvider); foreach (MultipartFileData fileData in streamProvider.FileData) { if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName)) { return Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"); } string fileName = fileData.Headers.ContentDisposition.FileName; if (fileName.StartsWith("\"") && fileName.EndsWith("\"")) { fileName = fileName.Trim('"'); } if (fileName.Contains(@"/") || fileName.Contains(@"\")) { fileName = Path.GetFileName(fileName); } File.Move(fileData.LocalFileName, Path.Combine(StoragePath, fileName)); } } return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"); } }
Abhilash P A- Pullelil
source share