经过几次测试,我发现我发布的服务器端代码示例是正确的。这是一个示例,已经剥离了任何身份验证/授权/错误处理代码:
经过几次测试,似乎我发布的作为示例的服务器端代码是正确的。以下是一个示例,已经剥离了任何身份验证/授权/错误处理代码:
[HttpPut]
[Route(@"api/storage/{*resourcePath?}")]
public async Task<HttpResponseMessage> PutFile(string resourcePath = "")
{
Stream fileContent = await this.Request.Content.ReadAsStreamAsync();
MediaTypeHeaderValue contentTypeHeader = this.Request.Content.Headers.ContentType;
string contentType =
contentTypeHeader != null ? contentTypeHeader.MediaType : "application/octet-stream";
bool isNew = await this._dal.SaveFile(resourcePath, contentType, fileContent);
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:
如此评论所述,如果您计划支持带有扩展名的文件名({filename}.{extension}
)而不强制客户端附加尾随斜杠,则需要修改您的web.config将IIS绑定到您的Web API应用程序以处理这些文件类型,因为默认情况下IIS将使用静态文件处理程序来处理看起来像文件名的内容(即URL中最后一个路径段包含点)。我的system.webServer
部分如下:
<system.webServer>
<handlers>
<clear />
<add name="FaviconStaticFile" path="/favicon.ico" verb="GET" modules="StaticFileModule" preCondition="integratedMode" resourceType="File" requireAccess="Read" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<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文件上传大小限制:
<location path="api/storage">
<system.web>
<httpRuntime maxRequestLength="200000000" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="200000000" />
</requestFiltering>
</security>
</system.webServer>
</location>
{filename}.{extension}
的文件名。有关详细信息,请参见此Stackoverflow问题:https://dev59.com/IWEi5IYBdhLWcg3wuuYk - Simon Elms