从链接下载pdf文件并保存在本地文件夹中 [英] Download pdf file from link and save in local file folder
问题描述
大家好,
我有网址,如果我在IE弹出窗口中打开显示下载pdf文件。如何以编程方式下载文件并将pdf文件保存在c#ASP.NET的本地文件夹中
提前致谢。
问候
Ganesh。
嗨...
看到这个,可能对你有用。
在aspx中 :
< div >
< asp:linkbutton id < span class =code-keyword> = LinkButtonDownloadPdf runat = server text = 下载PDF style = 颜色:海军;
font-weight:bold; onclick = LinkButtonDownloadPdf_Click / >
< / div >
在aspx.cs中 :
protected void LinkButtonDownloadPdf_Click( object sender,EventArgs e)
{
Response.ContentType = Application / pdf;
Response.AppendHeader( Content-Disposition, 附件; filename = Test_PDF.pdf);
Response.TransmitFile(Server.MapPath( 〜 /Files/Test_PDF.pdf));
Response.End();
}
谢谢你。
访问这里。
http://stackoverflow.com/questions/8590543/force-browser-to-download-pdf-document-instead-of-opening-it [ ^ ]
Pdf可以在asp.net中以两种方式下载:
*)使用脚本。
*)使用第三方pdf创建dll文件(iTextSharp)。
1)让我们先看看pdf下载使用脚本
步骤1:编写以下脚本你的.aspx页面。
<前lang =HTML> < script type = text / javascript >
function PrintGridData(){
var prtGrid = document .getElementById(' <%= GridView1.ClientID%>');
prtGrid.border = 0 ;
var prtwin = window .open(' ',' PrintGridViewData',' left = 100,top = 100,width = 1000,height = 1000,tollbar = 0,scrollbars = 1 ,状态= 0,可调整大小= 1' 跨度>);
prtwin。 document .write(prtGrid.outerHTML);
prtwin。 document .close();
prtwin.focus();
prtwin.print();
prtwin.close();
}
< / 脚本 >
第2步:现在添加一个简单的按钮来调用上面的函数。
< 输入 type = 按钮 id = btnPrint value = 转换为PDF önclick = PrintGridData() / >
2)现在让我们使用第三方dll下载pdf
步骤1:从以下网站下载iTextSharp dll
[ iTextSharp pdf dll Download ]
Step 2:通过以下简单步骤提取dll并将其添加到项目中
右键单击引用(Solution Explorer)
- >添加参考
- >导航到dll的提取位置
- >选择dll然后单击确定。
步骤3:现在在aspx页面添加一个按钮,如下所示:
< asp:按钮 id = pdf runat = server text = 转换为PDF onclick = pdf_Click xmlns:asp = #unknown / >
步骤4:现在在c#代码中添加以下命名空间
使用系统;
使用 System.IO;
使用 System.Web;
使用 System.Web.UI;
使用 iTextSharp.text;
使用 iTextSharp.text.pdf;
使用 MySql.Data.MySqlClient;
使用 System.Web.UI.WebControls;
使用 iTextSharp.text.html.simpleparser;
步骤5:现在编写单击按钮的代码(pdf_Click),如下所示
protected void pdf_Click( object sender,EventArgs e)
{
Response.ContentType = application / octet-stream;
Response.AddHeader( content-disposition, string .Format( attachment; filename = {0}, PDF.pdf));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter writer = new StringWriter();
HtmlTextWriter textwriter = new HtmlTextWriter(writer);
this .Page.RenderControl(textwriter);
StringReader reader = new StringReader(writer.ToString());
文件pdfdoc = new 文件(PageSize.A4,20f,20f,20f,20f);
// pdfdoc.AddHeader(标题,用户密码类型);
HTMLWorker worker = new HTMLWorker(pdfdoc);
PdfWriter.GetInstance(pdfdoc,Response.OutputStream);
pdfdoc.Open();
worker.Parse(读者);
pdfdoc.Close();
Response.Write(pdfdoc);
Response.End();
}
快乐编码。
Hi All,
I have URL if i open in IE popup window is displaying to download pdf file. How to programatically download file and save the pdf file in my local folder in c# ASP.NET
Thanks in advance.
Regards
Ganesh.
Hi...
See this one,may its useful to u.
In aspx:
<div> <asp:linkbutton id="LinkButtonDownloadPdf" runat="server" text="Download PDF" style="color: Navy; font-weight: bold;" onclick="LinkButtonDownloadPdf_Click" /> </div>
In aspx.cs:
protected void LinkButtonDownloadPdf_Click(object sender, EventArgs e) { Response.ContentType = "Application/pdf"; Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf"); Response.TransmitFile(Server.MapPath("~/Files/Test_PDF.pdf")); Response.End(); }
Thank u.
visit here.
http://stackoverflow.com/questions/8590543/force-browser-to-download-pdf-document-instead-of-opening-it[^]
Pdf can be downloaded in two ways in asp.net they are:
*) Using Script.
*) Using third party pdf creation dll files (iTextSharp).
1) Lets us first see pdf download using scripting
Step 1: Write the following script in ur .aspx page.
<script type="text/javascript"> function PrintGridData() { var prtGrid = document.getElementById('<%=GridView1.ClientID %>'); prtGrid.border = 0; var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1'); prtwin.document.write(prtGrid.outerHTML); prtwin.document.close(); prtwin.focus(); prtwin.print(); prtwin.close(); } </script>
Step 2: Now add a simple button to call the above function.
<input type="button" id="btnPrint" value="Convert To PDF" önclick="PrintGridData()" />
2) Now Lets download pdf using Third party dll
Step 1: Download the iTextSharp dll from the following website
[iTextSharp pdf dll Download]
Step 2: Extract the dll and add it to your project by following these simple steps
right click on references (Solution Explorer)
-> add reference
-> navigate to where the dll is extracted
-> Select the dll and click ok.
Step 3: Now add a button to your aspx page as follows:
<asp:button id="pdf" runat="server" text="Convert to PDF" onclick="pdf_Click" xmlns:asp="#unknown" />
Step 4: Now in c# code add the following namespaces
using System; using System.IO; using System.Web; using System.Web.UI; using iTextSharp.text; using iTextSharp.text.pdf; using MySql.Data.MySqlClient; using System.Web.UI.WebControls; using iTextSharp.text.html.simpleparser;
step 5: Now write the code for button click(pdf_Click) as follows
protected void pdf_Click(object sender, EventArgs e) { Response.ContentType = "application/octet-stream"; Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "PDF.pdf")); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter writer = new StringWriter(); HtmlTextWriter textwriter = new HtmlTextWriter(writer); this.Page.RenderControl(textwriter); StringReader reader = new StringReader(writer.ToString()); Document pdfdoc = new Document(PageSize.A4, 20f, 20f, 20f, 20f); //pdfdoc.AddHeader("Headers","User Password Type"); HTMLWorker worker = new HTMLWorker(pdfdoc); PdfWriter.GetInstance(pdfdoc, Response.OutputStream); pdfdoc.Open(); worker.Parse(reader); pdfdoc.Close(); Response.Write(pdfdoc); Response.End(); }
Happy coding.
这篇关于从链接下载pdf文件并保存在本地文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!