2016-07-22 56 views
1

PDFHelper.csPDF沒有下載在Chrome本地主機和Safari

byte[] bytes = memoryStream.ToArray(); 
       memoryStream.Close(); 
       HttpContext.Current.Response.Clear(); 
       HttpContext.Current.Response.AddHeader("content-length",      bytes.Length.ToString()); 
       HttpContext.Current.Response.ContentType = "application/pdf"; 
       HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf"); 
       //HttpContext.Current.Response.ContentType = "application/pdf"; 
       HttpContext.Current.Response.Buffer = true; 
       HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
       HttpContext.Current.Response.BinaryWrite(bytes); 
       HttpContext.Current.Response.End(); 

LoginFactory.js

 factory.getPDFReport = function (data) { 
     var headers = { 'Content-Type': 'application/pdf;' }; 
     var url = hostUrl + '/ReportsController/PDFReports'; 
     return $http.post(url, data, headers).then(
      function (results) { 
       return results;`**enter code here**` 
      }); 
    } 

這是我的代碼,但它是不工作的本地主機的Chrome和Safari瀏覽器。

回答

0

ASP.NET提供Response.TransmitFile用於下載文件。下面的代碼將做到這一點。

Response.Clear(); 
Response.ContentType = "application/pdf"; 
Response.AppendHeader("Content-Disposition", "attachment; filename=file_name.pdf"); 
Response.TransmitFile(file_path); 
Response.End(); 

UPDATE

如果你有Safari瀏覽器的問題,那麼在更改代碼AppendHeaderSetHeader

Response.SetHeader("Content-Disposition", "attachment; filename=file_name.pdf"); 
+0

感謝它正在工作......但它直接下載我希望它在新標籤中打開... – bhushan

+0

你在哪裏寫下了這部分代碼? (例如:按鈕onclick事件) –

+0

通常在您的asp.net對象上添加'target =「_ blank」'會在新窗口中觸發事件。 –