2015-06-23 23 views
5

Tôi muốn hiển thị hành động ASP.Net Web Api 2 bằng cách sử dụng động từ HTTP PUT để tải tệp lên. Điều này kết hợp với mô hình REST của chúng tôi vì API đại diện cho một hệ thống tệp từ xa (tương tự như WebDAV, nhưng thực sự đơn giản), vì vậy máy khách chọn tên tài nguyên (do đó PUT là lý tưởng và POST không phải là sự lựa chọn hợp lý).Tải tệp lên bằng cách sử dụng động từ PUT trong ASP.Net Web Api 2

Tài liệu Web Api mô tả how to upload files using multipart/form-data forms, nhưng không mô tả cách thực hiện bằng các phương pháp PUT.

Bạn sẽ sử dụng cái gì để kiểm tra một API như vậy (các biểu mẫu ghép kênh HTML không cho phép động từ PUT)? việc thực hiện máy chủ sẽ trông giống như thực hiện nhiều phần dữ liệu được mô tả trong the web api documentation (sử dụng MultipartStreamProvider), hoặc nó sẽ giống như thế này:

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

Trả lời

8

Sau một vài thử nghiệm có vẻ như mã server-side tôi được đăng là một ví dụ là đúng . Dưới đây là một ví dụ, tước ra của bất kỳ xử lý mã xác thực/giấy phép/lỗi:

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

Một ứng dụng giao diện điều khiển đơn giản là đủ để kiểm tra nó (sử dụng thư viện Web Api khách hàng):

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

Sẽ thú vị để xem làm thế nào để viết một bài kiểm tra đơn vị thích hợp cho việc này, thay vì dựa vào việc chạy một Máy chủ HTTP. – James

+0

Tôi gặp rất nhiều sự cố khi làm việc này vì tôi vẫn gặp lỗi 404. Hóa ra bạn có thể cần phải thay đổi web.config để chấp nhận tên tệp của biểu mẫu '{filename}. {Extension}'. Xem câu hỏi Stackoverflow này để biết chi tiết: http://stackoverflow.com/questions/20998816/dot-character-in-mvc-web-api-2-for-request-such-as-api-people-staff-45287 –

Các vấn đề liên quan