Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firefox Azure Blob在导航到url时始终下载_Firefox_Google Chrome_Azure_Blob_Azure Storage - Fatal编程技术网

Firefox Azure Blob在导航到url时始终下载

Firefox Azure Blob在导航到url时始终下载,firefox,google-chrome,azure,blob,azure-storage,Firefox,Google Chrome,Azure,Blob,Azure Storage,在我们的应用程序中,我们允许用户将文档上载到windows azure blob存储帐户。上传文档或图像后,会为其分配一些url(https://name.blob.core.windows.net/container/file-name.jpg). 如果文档是图像、pdf或可由浏览器呈现的文件,我们将尝试在浏览器中显示它,而无需用户下载该文件。如果我们只是打开一个新窗口或选项卡,并将用户指向IE中的blob uri,那么图像或pdf将在浏览器中正确呈现。但是,如果我们试图在Chrome、Fi

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


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

这是因为您没有设置blob的属性(默认为application/octet stream,在大多数浏览器中会触发下载)。如果希望PDF文件正确显示,则需要将PDF文件的内容类型更改为应用程序/PDF(图像/jpeg用于jpeg文件)

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

   blob.Properties.ContentType = "application/pdf";
//按扩展名获取文件的内容类型

    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;
    }
使用此选项可以在保存blob时设置其内容类型


对于通过PowerShell上载的文件,请在上载过程中使用以下语法设置内容类型

Set-AzureStorageBlobContent -File <localFilePath> -Container <containerName> -Properties @{"ContentType"="text/plain"} -Context $ctx
Set AzureStorageBlobContent-File-Container-Properties@{“ContentType”=“text/plain”}-Context$ctx
在上面,我将blob内容类型设置为text/plain,这在上载将与模板一起使用的JSON和HTML文件时非常有用。更多内容类型标题值的列表。

如果使用Azure SDK(12.x+),则需要使用传入上载方法的BlobHttpHeader(而不是blob.Properties.ContentType)进行更改

例如:

    var header = new BlobHttpHeaders();
    header.ContentType = "image/jpeg";

    var response = await blobClient.UploadAsync(stream, header);

如果使用HttpPostedFileBase,它已经包含内容类型attr ex:
blockBlob.Properties.ContentType=HttpPostedFileBase.ContentType工作正常;我最终决定自动确定文件的mime类型:
blob.Properties.ContentType=MimeMapping.GetMimeMapping(path)该url不再有效