是否可以使用GUID (或其他名称)保存blob,但是当用户请求文件URI http://me.blob.core.windows.net/mycontainer/9BB34783-8F06-466D-AC20-37A03E504E3F时,下载会使用友好的名称,例如MyText.txt?
发布于 2012-04-25 18:16:16
允许用户在Windows Azure中下载文件(blobs)可以通过4种方式完成;
下面是一段代码片段,它将文件流式传输给用户,并覆盖文件名。
//Retrieve filenname from DB (based on fileid (Guid))
// *SNIP*
string filename = "some file name.txt";
//IE needs URL encoded filename. Important when there are spaces and other non-ansi chars in filename.
if (HttpContext.Current.Request.UserAgent != null && HttpContext.Current.Request.UserAgent.ToUpper().Contains("MSIE"))
filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8).Replace("+", " ");
context.Response.Charset = "UTF-8";
//Important to set buffer to false. IIS will download entire blob before passing it on to user if this is not set to false
context.Response.Buffer = false;
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
context.Response.AddHeader("Content-Length", "100122334"); //Set the length the file
context.Response.ContentType = "application/octet-stream";
context.Response.Flush();
//Use the Azure API to stream the blob to the user instantly.
// *SNIP*
fileBlob.DownloadToStream(context.Response.OutputStream);有关更多信息,请参阅此博文:http://blog.degree.no/2012/04/downloading-blobs-from-windows-azure/
发布于 2015-06-10 21:28:31
现在可以通过在生成共享访问签名时设置content-disposition头部:
string sasBlobToken = blob.GetSharedAccessSignature(sharedPolicy, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=" + friendlyFileName
});
string downloadLink = blob.Uri + sasBlobToken;发布于 2013-12-03 05:26:04
从2013年11月起,Windows Azure Blob存储中现在支持content-disposition标头。您可以在创建blob时分配标头,也可以通过共享访问签名分配标头。
我修改了我的save document方法,以获取一个可选参数,该参数是要下载的友好名称。
public void SaveDocument(Stream documentToSave, string contentType, string containerName, string url, string friendlyName = null)
{
var container = GetContainer(containerName);
container.CreateIfNotExists();
var blob = container.GetBlockBlobReference(url);
blob.Properties.ContentType = contentType;
if (friendlyName != null)
blob.Properties.ContentDisposition = "attachment; filename=" + friendlyName;
blob.UploadFromStream(documentToSave);
}更多信息:http://blog.simontimms.com/2013/12/02/content-disposition-comes-to-azure/
https://stackoverflow.com/questions/7027656
复制相似问题