2012-08-04 56 views
16

在我們的應用程序中,我們授予用戶將文檔上傳到Windows Azure Blob存儲帳戶的能力。上傳文件或圖像後,它會分配一些URL(https://name.blob.core.windows.net/container/file-name.jpg)。如果文檔是一個圖像或一個pdf或一些可以通過瀏覽器呈現的文件,我們正試圖在瀏覽器中顯示它,而不需要用戶下載文件。如果我們只是打開一個新的窗口或標籤,並將用戶引導至IE中的blob uri,則圖像或pdf將在瀏覽器中正確呈現。但是,如果我們試圖在Chrome,FireFox或Safari中打開一個指向uri的新窗口,它只是下載文件,而不是在瀏覽器中顯示它。Azure Blob總是在導航到url時下載

有沒有辦法強制後三個瀏覽器只顯示文件,而不是下載它?

回答

30

這是因爲你沒有設置blob的content type屬性(默認是application/octet-stream,這會在大多數瀏覽器中觸發下載)。如果您希望PDF文件正確顯示,則需要將PDF文件的內容類型更改爲application/pdf(jpeg文件的圖像/ jpeg)。

您可以使用Azure存儲瀏覽器,Cloud Storage Studio,CloudBerry,CloudXplorer等常用工具或使用SDK更改內容類型。請注意,上傳文件後,其中一些工具會自動將內容類型設置爲正確的內容類型。

+1

的網址不再有效 – spuder 2017-03-01 18:08:40

19
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的內容類型,同時節省它獲取文件的內容類型。

Set Content-type of media files stored on Blob

+2

如果使用HttpPostedFileBase,它已經包含內容類型ATTR 例如: ''blockBlob.Properties.ContentType = httpPostedFileBase.ContentType;'' – user1686407 2016-07-11 03:42:21

+1

這完美地工作;我最終決定了這一點,以自動確定我的文件的MIME類型: 'blob.Properties.ContentType = MimeMapping.GetMimeMapping(path);' – 2017-02-16 16:26:47

0

對於那些通過PowerShell的上傳文件,請使用以下語法在上傳過程中設置內容類型。

Set-AzureStorageBlobContent -File <localFilePath> -Container <containerName> -Properties @{"ContentType"="text/plain"} -Context $ctx 

上面我設置BLOB內容類型爲text/plain的,有用的上傳JSON和將與模板使用HTML文件時。更多內容類型標題值列表here