Asp.net
Chrome 瀏覽器不顯示 HTTP 處理程序生成的圖像
基本上我有一個網站可以呈現一些文件的 HTML 預覽(主要是辦公室)。生成的 HTML 片段包含在同一網站返回的頁面中,但是圖像由來自另一個網站的 HTTP 處理程序返回,並帶有以下連結:
<img width="50" height="50" src="http://portal/Service/GetFile.asxh?id=123&inline=true">出於某種原因,除 Chrome 之外的所有瀏覽器(例如 IE6/7/8、Firefox、Opera、Safari)都顯示一切正常,但是對於這些圖像,Chrome 顯示“損壞的圖像”圖示。如果我選擇“在新選項卡中打開圖像”,則圖像顯示得很好。
編輯我以為我已經解決了這個問題,但顯然打開 Fiddler 後它工作正常。
我在程式碼中留下了 context.Response=“utf-8” ,但刪除它沒有區別。
標題:
HTTP/1.1 200 OK Date: Wed, 05 Jan 2011 14:26:57 GMT Server: Microsoft-IIS/6.0 MicrosoftOfficeWebServer: 5.0_Pub X-Powered-By: ASP.NET X-AspNet-Version: 4.0.30319 Transfer-Encoding: chunked Cache-Control: no-cache Pragma: no-cache Expires: -1 Content-Type: image/jpeg程式碼:
context.Response.ContentType = file.ContentType; context.Response.Cache.SetCacheability(HttpCacheability.NoCache); byte[] buff = new byte[BuffSize]; using (var stream = repository.GetFileContentsAsStream(file.ContentId)) { int bytesRead; do { bytesRead = stream.Read(buff, 0, BuffSize); if (bytesRead > 0) { context.Response.OutputStream.Write(buff, 0, bytesRead); } } while (bytesRead > 0); } context.Response.Flush(); context.Response.Close();
我很確定 Chrome 需要為圖像設置長度,因此在處理圖像時嘗試將 Content-Length 標頭添加到您的響應中。
這是 context.Response.Close(); 鉻不喜歡它。擺脫那條線,一切都會好起來的。我為此奮鬥了幾個月。