在ASP.Net网页API使用2动词PUT文件上传 [英] Upload file using PUT verb in ASP.Net Web Api 2

查看:232
本文介绍了在ASP.Net网页API使用2动词PUT文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用HTTP PUT动词上传文件揭露一个ASP.Net网页API 2的动作。这是一致的我们的REST模型的API代表的远程文件系统(类似于WebDAV的,但真的简体),所以客户选择资源名称(这样说就是理想和POST不是一个合乎逻辑的选择)。



在Web API文档描述的如何使用的multipart / form-data的形式来上传文件,但没有说明如何使用PUT方法来做到这一点。



你会怎样用它来测试这样的API(HTML多联表格不允许PUT动词)?将服务器实现看起来像的网络API文档(使用 MultipartStreamProvider ),还是应该是这样的:

  [HttpPut] 
公共异步任务< HttpResponseMessage> PUTFILE(字符串resourcePath)
{
流fileContent =等待this.Request.Content.ReadAsStreamAsync();
布尔是否新款=等待this._storageManager.UploadFile(resourcePath,fileContent);
如果(是否新款)
{
返回this.Request.CreateResponse(HttpStatusCode.Created);
}
,否则
{
返回this.Request.CreateResponse(HttpStatusCode.OK);
}
}


解决方案

在一些测试中,它似乎是服务器端的代码我张贴作为一个例子是正确的。下面是一个例子,剥夺了任何身份验证/授权/错误处理代码:

  [HttpPut] 
[路线(@API /存储/ {* resourcePath?})]
公共异步任务< HttpResponseMessage> PUTFILE(字符串resourcePath =)
{从请求
流fileContent =
//提取数据等待this.Request.Content.ReadAsStreamAsync();
MediaTypeHeaderValue contentTypeHeader = this.Request.Content.Headers.ContentType;
串的contentType =
contentTypeHeader!= NULL? contentTypeHeader.MediaType:应用程序/八位字节流;

//将文件保存到底层存储
布尔是否新款=等待this._dal.SaveFile(resourcePath,则contentType,fileContent);

//返回相应的HTTP状态码
如果(是否新款)
{
返回this.Request.CreateResponse(HttpStatusCode.Created);
}
,否则
{
返回this.Request.CreateResponse(HttpStatusCode.OK);
}
}

一个简单的控制台应用程序是不够测试(用网页API客户端库):\temp\testfile.txt:使用(VAR fileContent =新的FileStream(@C

  使用,FileMode.Open))
(VAR的客户=新的HttpClient())
{
变种内容=新StreamContent(fileContent);
content.Headers.ContentType =新MediaTypeHeaderValue(text / plain的);
client.BaseAddress =新的URI(HTTP://本地主机:81);
HttpResponseMessage响应=
等待client.PutAsync(@/ API /存储/ TESTFILE.TXT内容);
}


I would like to expose an ASP.Net Web Api 2 action using the HTTP PUT verb to upload files. This is coherent with our REST model as the API represents a remote file system (similar to WebDAV, but really simplified), so the client chooses the resource names (thus PUT is ideal and POST is not a logical choice).

The Web Api documentation describes how to upload files using multipart/form-data forms, but does not describe how to do it using PUT methods.

What would you use to test such an API (HTML multipart forms don't allow PUT verbs)? Would the server implementation look like the multipart implementation described in the web api documentation (using the MultipartStreamProvider), or should it look like this:

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

解决方案

After a few tests it seems the server-side code I posted as an example is correct. Here is an example, stripped out 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 app is enough to test it (using 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);
}

这篇关于在ASP.Net网页API使用2动词PUT文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆