公开下载Azure blob时的友好文件名
可以使用GUID的名称(或其他任何东西)来保存一个Blob,但是当用户请求文件URI
http://me.blob.core.windows.net/mycontainer/9BB34783-8F06-466D-AC20-37A03E504E3F时,下载文件会以友好的名字命名. MyText.txt?
使用户能够在Windows Azure中下载文件(blob)可以通过4种方式完成;
>直接下载 – 将容器的访问权限设置为公共读取访问权限或完全公共访问权限,并将URL公开给最终用户.这种方法的缺点显然是安全的 – 当URL暴露时,你无法控制访问.还没有办法检测下载,并且在下载之前/之后执行代码. 这是一个代码片段,将文件流式传输给用户,并覆盖文件名. //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/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |