2011-11-28 26 views
11

這是下載文件的代碼。如何在新選項卡或窗口中打開PDF文件而不是下載它(使用asp.net)?

System.IO.FileStream fs = new System.IO.FileStream(Path+"\\"+fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); 
byte[] ar = new byte[(int)fs.Length]; 
fs.Read(ar, 0, (int)fs.Length); 
fs.Close(); 

Response.AddHeader("content-disposition", "attachment;filename=" + AccNo+".pdf"); 
Response.ContentType = "application/octectstream"; 
Response.BinaryWrite(ar); 
Response.End(); 

當執行此代碼時,它會要求用戶打開或保存該文件。代替這個,我需要打開一個新標籤或窗口並顯示該文件。我怎樣才能做到這一點?

注:

文件將沒有必要設在網站上的文件夾。它可能位於其他文件夾中。

+4

你可以試試'內容處置:inline',而不是'attachment' - [參見這篇文章(http://dotanything.wordpress.com/2008/05/30/content-disposition-attachment-vs-inline /) –

+0

這篇文章可以幫助你http://aspalliance.com/259_downloading_files__forcing_the_file_download_dialog –

回答

3

這可能有助於

Response.Write("<script>"); 
Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');"); 
Response.Write("</script>"); 
+0

我如何調用下載文件到這個?這是我的問題。 – smilu

+0

@smilu您是否保存該文件或者只是爲了閱讀而創建它? – Karthik

+0

我應該爲閱讀創建它,稍後它也可以保存。問題是我不能給該文件的文件或路徑的原始名稱。所以我必須執行上述步驟才能下載它,然後用另一個名稱打開它。 – smilu

14

不必加載流成字節數組,並將其寫入到響應流中,你應該看看HttpResponse.TransmitFile

Response.ContentType = "Application/pdf"; 
Response.TransmitFile(pathtofile); 

如果你想在PDF要在新窗口中打開,您必須在新窗口中打開下載頁面,例如:

<a href="viewpdf.aspx" target="_blank">View PDF</a> 
+0

第一步我有興趣知道更多。第二步在我的程序中不實用。 bcz我無法將原始路徑發送給它。使用第一步可以將文件重定向到另一個窗口? – smilu

+3

您無法從服務器端打開一個新窗口。您必須在新窗口中打開下載頁面,並在該窗口中使用TransmitFile將文件返回給用戶。 – janzi

+0

你的鏈接是什麼樣的?爲什麼你不能添加一個目標?你可以用JavaScript添加,如果由於某種原因,你不能改變的一個標籤的來源(例如它爲您生成) –

-4

使用此代碼。這就像一個冠軍。

Process process = new Process(); 
process.StartInfo.UseShellExecute = true; 
process.StartInfo.FileName = outputPdfFile; 
process.Start(); 
+0

這個方法我試過和它的作品真的很好,謝謝:) – JohnnyBizzle

+9

你的C#代碼在服務器上運行。 因此,Process.Start會在服務器上啓動進程,而不是您的計算機。 這是根本不可能爲你在客戶端上直接啓動一個進程。 – dnxit

+3

是的,這個答案是完全錯誤的。 –

1

您必須創建另一個頁面或通用處理程序與代碼來生成您的pdf。然後,該事件被觸發,並且該人被重定向到該頁面。

4
Response.ContentType = contentType; 
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName); 
Response.BinaryWrite(fileContent); 

而且

<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../> 

在javascript:

<script type="text/javascript"> 
    function openInNewTab() { 
     window.document.forms[0].target = '_blank'; 
     setTimeout(function() { window.document.forms[0].target = ''; }, 0); 
    } 
</script> 

小心重置目標,否則所有其他呼叫像Response.Redirect將在新標籤中打開,這可能是不你想要什麼。

0

您可以從您的MVC操作返回FileResult。

********************* MVC行動************

public FileResult OpenPDF(parameters) 
    { 
     //code to fetch your pdf byte array 
     return File(pdfBytes, "application/pdf"); 
    } 

*** *********** JS **************

使用formpost你的數據發佈到行動

var inputTag = '<input name="paramName" type="text" value="' + payloadString + '">'; 
    var form = document.createElement("form"); 
    jQuery(form).attr("id", "pdf-form").attr("name", "pdf-form").attr("class", "pdf-form").attr("target", "_blank"); 
    jQuery(form).attr("action", "/Controller/OpenPDF").attr("method", "post").attr("enctype", "multipart/form-data"); 
    jQuery(form).append(inputTag); 
    document.body.appendChild(form); 
    form.submit(); 
    document.body.removeChild(form); 
    return false; 

您需要創建一個表單來發布你的數據,追加你的數據,發佈你的數據並刪除你的文檔主體。

但是,表單發佈不會將數據發佈到新標籤只有在EDGE瀏覽器。但GET請求工作,它只是打開新的標籤頁包含查詢字符串爲您的操作參數的URL。

相關問題