2015-06-23 10 views
5

Dosyaları yüklemek için HTTP PUT fiilini kullanarak bir ASP.Net Web Api 2 eylemini açığa çıkarmak istiyorum. Bu API, bir uzak dosya sistemini (WebDAV'a benzer, ancak gerçekten basitleştirilmiş) temsil ettiği için REST modelimiz ile uyumludur, dolayısıyla istemci kaynak isimlerini seçer (böylece PUT idealdir ve POST mantıklı bir seçim değildir).ASP.Net Web Api 2'de PUT fiilini kullanarak dosya yükleme

Web Api belgeleri, how to upload files using multipart/form-data forms'u açıklar, ancak PUT yöntemlerini kullanarak nasıl yapılacağını açıklamaz.

Böyle bir API'yi sınamak için ne kullanırsınız (HTML çoktan formları PUT fiillerine izin vermez)? Sunucu uygulaması (MultipartStreamProvider kullanarak) the web api documentation açıklanan çok parçalı uygulanması gibi görünecektir, yoksa bu gibi görünmelidir:

ben bir örnek olarak yayınlanmıştır sunucu tarafı kodu görünüyor birkaç testlerden sonra
[HttpPut] 
public async Task<HttpResponseMessage> PutFile(string resourcePath) 
{ 
    Stream fileContent = await this.Request.Content.ReadAsStreamAsync(); 
    bool isNew = await this._storageManager.UploadFile(resourcePath, fileContent); 
    if (isNew) 
    { 
     return this.Request.CreateResponse(HttpStatusCode.Created); 
    } 
    else 
    { 
     return this.Request.CreateResponse(HttpStatusCode.OK); 
    } 
} 

cevap

8

doğrudur .

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); 
} 
+1

O olacaktır:

[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); } } 

Basit konsol uygulaması (Web Api istemci kütüphanelerini kullanarak) test etmek yeterlidir: Burada herhangi bir kimlik doğrulama/yetkilendirme/hata işleme kod dışarı çıkardı bir örnek vardır HTTP Sunucusu'nu kullanmaktan ziyade, bunun için uygun bir birim testi nasıl yazılacağını görmek ilginç. – James

+0

404 hatası aldığım için bunu çalışmak için çok fazla sorun yaşadım. Görünüşe göre, '{dosyaadı}. {Extension} 'biçimindeki dosya adlarını kabul etmek için web.config dosyasını değiştirmeniz gerekebilir. Ayrıntılar için şu Stackoverflow sorusuna bakın: http://stackoverflow.com/questions/20998816/dot-character-in-mvc-web-api-2-for-request-such-as-api-people-staff-45287 –