微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在ASP.Net Web Api 2中使用PUT动词上传文件

我想使用HTTP PUT动词公开ASP.Net Web Api 2动作来上传文件.这与我们的REST模型一致,因为API代表一个远程文件系统(类似于WebDAV,但实际上是简化的),因此客户端选择资源名称(因此PUT是理想的,POST不是一个合理的选择).

Web api文档描述了how to upload files using multipart/form-data forms,但没有描述如何使用PUT方法.

您将使用什么来测试这样的API(HTML多部分表单不允许PUT动词)?服务器实现是否类似于the web api documentation中描述的多部分实现(使用MultipartStreamProvider),或者应该如下所示:

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

解决方法

经过几次测试后,我发布的服务器端代码似乎是正确的.这是一个示例,从任何身份验证/授权/错误处理代码删除
[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);
    }
}

一个简单的控制台应用程序足以测试它(使用Web Api客户端库):

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

编辑2018-05-09:

this comment中所述,如果您计划支持带扩展名({filename}.{extension})的文件名而不强制客户端附加尾部斜杠,则需要修改web.config以将IIS绑定到Web api这些文件类型的应用程序,认情况下IIS将使用静态文件处理程序来处理看起来像文件名的内容(即包含点的最后一个路径段的URL).我的system.webServer部分如下所示:

<system.webServer>
    <handlers>
      <!-- Clear all handlers,prevents executing code file extensions or returning any file contents. -->
      <clear />
      <!-- Favicon static handler. -->
      <add name="FaviconStaticFile" path="/favicon.ico" verb="GET" modules="StaticFileModule" preCondition="integratedMode" resourceType="File" requireAccess="Read" />
      <!-- By default,only map extensionless URLs to ASP.NET -->
      <!-- (the "*." handler mapping is a special Syntax that matches extensionless URLs) -->
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <!-- API endpoints must handle path segments including a dot -->
      <add name="ExtensionIncludedUrlHandler-Integrated-4.0" path="/api/storage/*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

请注意,由于各种限制,某些文件名将无法使用.例如,您无法命名路径段.或者..因为RFC要求替换它,Azure托管服务将不允许冒号作为路径段的最后一个字符,并且IIS禁止一组字符.

您可能还希望增加IIS / ASP.NET文件上载大小限制:

<!-- Path specific settings -->
<location path="api/storage">
  <system.web>
    <httpRuntime maxRequestLength="200000000" />
  </system.web>
  <system.webServer>
    <security>
      <requestfiltering>
        <requestLimits maxAllowedContentLength="200000000" />
      </requestfiltering>
    </security>
    </system.webServer>
</location>

原文地址:https://www.jb51.cc/csharp/244358.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐