导航到URL时,Azure Blob始终会下载

Nic*_*sen 26 firefox google-chrome blob azure azure-storage

在我们的应用程序中,我们为用户提供了将文档上传到windows azure blob存储帐户的能力.上传文档或图像后,它会被分配一些网址(https://name.blob.core.windows.net/container/file-name.jpg).如果文档是图像或pdf或某些可由浏览器呈现的文件,我们试图在浏览器中显示它而无需用户下载文件.如果我们只是打开一个新窗口或选项卡并将用户引导到IE中的blob uri,则图像或pdf会在浏览器中正确呈现.但是,如果我们尝试在Chrome,FireFox或Safari中打开一个指向uri的新窗口,它只会下载文件而不是在浏览器中显示它.

有没有办法强制后三个浏览器只显示文件而不是下载它?

San*_*tia 47

这是因为您没有设置blob 的内容类型属性(默认为application/octet-stream,它会在大多数浏览器中触发下载).如果要正确显示PDF文件,则需要将PDF文件的内容类型更改为application/pdf(jpeg文件的image/jpeg).

您可以使用Azure Storage Explorer,Cloud Storage Studio,CloudBerry,CloudXplorer等常用工具或使用SDK更改内容类型.请注意,上传文件后,其中一些工具会自动将内容类型设置为正确的内容类型.

  • 网址不再有效 (2认同)

sud*_*u63 25

   blob.Properties.ContentType = "application/pdf";
Run Code Online (Sandbox Code Playgroud)

//按扩展名获取文件的内容类型

    public static string GetFileContentType(string FilePath)
    {
        string ContentType = String.Empty;
        string Extension = Path.GetExtension(FilePath).ToLower();

        switch (Extension)
        {
            case ConstantUtility.FILE_EXTENSION_PDF:
                ContentType = "application/pdf";
                break;
            case ConstantUtility.FILE_EXTENSION_TXT:
                ContentType = "text/plain";
                break;
            case ConstantUtility.FILE_EXTENSION_BMP:
                ContentType = "image/bmp";
                break;
            case ConstantUtility.FILE_EXTENSION_GIF:
                ContentType = "image/gif";
                break;
            case ConstantUtility.FILE_EXTENSION_PNG:
                ContentType = "image/png";
                break;
            case ConstantUtility.FILE_EXTENSION_JPG:
                ContentType = "image/jpeg";
                break;
            case ConstantUtility.FILE_EXTENSION_JPEG:
                ContentType = "image/jpeg";
                break;
            case ConstantUtility.FILE_EXTENSION_XLS:
                ContentType = "application/vnd.ms-excel";
                break;
            case ConstantUtility.FILE_EXTENSION_XLSX:
                ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
            case ConstantUtility.FILE_EXTENSION_CSV:
                ContentType = "text/csv";
                break;
            case ConstantUtility.FILE_EXTENSION_HTML:
                ContentType = "text/html";
                break;
            case ConstantUtility.FILE_EXTENSION_XML:
                ContentType = "text/xml";
                break;
            case ConstantUtility.FILE_EXTENSION_ZIP:
                ContentType = "application/zip";
                break;
            default:
                ContentType = "application/octet-stream";
                break;

        }


        return ContentType;
    }
Run Code Online (Sandbox Code Playgroud)

使用此选项可在保存时设置blob的内容类型.

设置存储在Blob上的媒体文件的内容类型

  • 如果你使用HttpPostedFileBase,它已经包含内容类型attr ex:``blockBlob.Properties.ContentType = httpPostedFileBase.ContentType;`` (2认同)
  • 那很好。我最终解决了这个问题,以自动确定文件的MIME类型:`blob.Properties.ContentType = MimeMapping.GetMimeMapping(path);` (2认同)