簡體   English   中英

HttpContext.Current.Response.AddHeader()未設置Content-Type標頭

[英]HttpContext.Current.Response.AddHeader() not setting Content-Type header

我正在使用第三方軟件從html文檔渲染PDF。 我創建了一個小型測試項目,並使用<asp:Button>OnClick事件,可以從目錄中讀取html文檔並將其呈現為PDF,而沒有任何問題。

響應創建如下:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
HttpContext.Current.Response.AddHeader("Content-Disposition", 
    String.Format("{0}; filename=Test.pdf;", "inline" ));
HttpContext.Current.Response.BinaryWrite(pdfBuffer);
HttpContext.Current.ApplicationInstance.CompleteRequest();

當我查看Chrome中的響應時,可以看到Content-Type的設置正確:

內容處置:內聯; 文件名= HtmlToPdf.pdf;

內容長度:101482

內容類型:應用程序/ PDF

我試圖將以上內容轉移到我當前的項目中。 唯一的區別是,我在DevExpress網格視圖中使用了一個自定義按鈕,而不是一個<asp:Button> 我最初處理了回調面板中的自定義按鈕單擊,但是未設置Content-Type。 在Chrome中查看響應證明了這一點:

內容處置:內聯; 文件名= 5.pdf;

內容編碼:gzip

內容長度:149015

內容類型:text / plain的; 字符集= utf-8的

我也嘗試過使用gvDocuments.CustomButtonCallback += new ASPxGridViewCustomButtonCallbackEventHandler(gvDocuments_CustomButtonCallback); 事件,但仍未設置Content-Type。

關於在上述情況下為什么無法設置Content-Type的任何人都有想法?

你可以試試

HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();

response.ContentType =“應用程序/ pdf”;

response.AddHeader("Content-Disposition", "attachment; filename=" + yourFileName + ".pdf");
stream.WriteTo(response.OutputStream);
response.Flush();
response.End();

希望它能工作:)

Dim Resp As HttpResponse = HttpContext.Current.Response
Resp.Headers.Remove("X-Frame-Options")
Resp.AppendHeader("X-Frame-Options", "SAMEORIGIN")

通過在頁面load / init方法中添加以上行,將添加http響應標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM