我正在Azure托管的网站上工作,该网站具有下载功能。为减少我们服务器的负载,使用共享访问签名完成下载。但是,在Safari下载文件时,文件名用单引号括起来,如myFile.txt下载为“myFile.txt'”。这使得下载的拉链必须由客户端重命名,以便可以提取内容。
生成共享访问签名的代码如下:
CloudBlockBlob blob = container.GetBlockBlobReference(Helpers.StringHelper.TrimIfNotNull(blobName));
if (!blob.Exists())
{
return string.Empty;
}
var sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddSeconds(-5);
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.Add(duration);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write;
var headers = new SharedAccessBlobHeaders();
string filename = blobName;
if (filename.Contains("/"))
{
filename = blobName.Substring(blobName.LastIndexOf("/") + 1, blobName.Length - blobName.LastIndexOf("/") - 1);
}
headers.ContentDisposition = "attachment; filename='" + filename + "'";
//Generate the shared access signature on the blob, setting the constraints directly on the signature.
string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints, headers);
//Return the URI string for the container, including the SAS token.
return blob.Uri + sasBlobToken;
此代码在Chrome,Firefox和IE中运行良好。标题中是否有我遗漏的东西?我唯一要修改的是内容处理。
答案 0 :(得分:3)
您应该在HTTP标头中对引用的字符串使用双引号,如RFC2616中所述。
所以替换
headers.ContentDisposition = "attachment; filename='" + filename + "'";
带
headers.ContentDisposition = "attachment; filename=\"" + filename + "\"";