我有一个带有 Azure Blob 存储的 WCF REST 服务。我不想将我的服务用作流的代理,因此我想将客户端的请求重定向到绝对 blob url,但它会重定向到文件并尝试直接打开它。我想要的只是当服务器将请求重定向到绝对 blob url 时开始下载过程。我尝试过的代码如下:
var sas = blob.GetSharedAccessSignature(new SharedAccessPolicy
{
Permissions = SharedAccessPermissions.Read,
SharedAccessStartTime = DateTime.UtcNow,
SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(1)
});
WebOperationContext.Current.OutgoingResponse.ContentType = blob.Properties.ContentType;
WebOperationContext.Current.OutgoingResponse.ContentLength = blob.Properties.Length;
WebOperationContext.Current.OutgoingResponse.Location = blob.Uri.AbsoluteUri+sas;
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Redirect;
附注我尝试将状态代码更改为“重定向”、“已移动”等,但没有任何变化。
更新 如果我将 ContentType 更改为 application/octet-stream,它在 IE 中的结果如下:
<base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
...
</base64Binary>
最佳答案
可能与 this question 重复.
由于 Stive Marx 表示无法控制 Azure Lbob 的“内容处置” header ,因此无法强制浏览器 (IE) 下载该文件。将内容类型设置为“application/octet-stream”可能会强制某些浏览器下载文件,而不是打开它,但显然它不适用于 IE。
关于wcf - 使用 Azure Blob 存储从 WCF REST 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8764400/