After several tests, it seems that the server-side code that I posted as an example is correct. Here is an example devoid of any authentication / authorization / error handling code:
[HttpPut] [Route(@"api/storage/{*resourcePath?}")] public async Task<HttpResponseMessage> PutFile(string resourcePath = "") { // Extract data from request Stream fileContent = await this.Request.Content.ReadAsStreamAsync(); MediaTypeHeaderValue contentTypeHeader = this.Request.Content.Headers.ContentType; string contentType = contentTypeHeader != null ? contentTypeHeader.MediaType : "application/octet-stream"; // Save the file to the underlying storage bool isNew = await this._dal.SaveFile(resourcePath, contentType, fileContent); // Return appropriate HTTP status code if (isNew) { return this.Request.CreateResponse(HttpStatusCode.Created); } else { return this.Request.CreateResponse(HttpStatusCode.OK); } }
A simple console application is enough to test it (using the Web Api client libraries):
using (var fileContent = new FileStream(@"C:\temp\testfile.txt", FileMode.Open)) using (var client = new HttpClient()) { var content = new StreamContent(fileContent); content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); client.BaseAddress = new Uri("http://localhost:81"); HttpResponseMessage response = await client.PutAsync(@"/api/storage/testfile.txt", content); }
source share