简体   繁体   English

使用C#在特定位置保存pdf

[英]Save pdf on a particular location using C#

Here is my C# Code : 这是我的C#代码:

protected void Button1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=" + Session["pdf_name"] + ".pdf";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/pdf";
        StringWriter s_tw = new StringWriter();
        HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
        Label1.Style.Add("font-size", "16pt");
        Label2.Style.Add("font-size", "16pt");
        Label3.Style.Add("font-size", "16pt");
        h_textw.AddStyleAttribute("font-size", "8pt");
        h_textw.AddStyleAttribute("color", "Black");
        Panel1.RenderControl(h_textw);
        Document doc = new Document();
        PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        StringReader s_tr = new StringReader(s_tw.ToString());
        HTMLWorker html_worker = new HTMLWorker(doc);
        html_worker.Parse(s_tr);
        doc.Close();
        Response.Write(doc);
}

I want to save this pdf in this Path "H:\\new\\web\\pdf_filename.pdf" 我想将此pdf保存在此路径“ H:\\ new \\ web \\ pdf_filename.pdf”中

I am using ASP.Net C#. 我正在使用ASP.Net C#。

You cannot specify where on the client machine the downloaded file will be saved. 您无法指定下载的文件将在客户端计算机上的何处保存。

If you want that type of functionality, you will need to run a plugin in the browser clientside that has access to the local file system. 如果要使用这种功能,则需要在浏览器客户端中运行有权访问本地文件系统的插件。

The browser will take the filename you specify in your attachment header value as a suggestion, but even that can be overridden by the user when he actually saves it. 浏览器将使用您在附件标题值中指定的文件名作为建议,但是即使用户实际保存该文件名时,也可以覆盖该文件名。

If you want to save it on the server, you only have to save it somewhere local (to the server) using any of the file-related methods or types. 如果要将其保存在服务器上,则只需使用任何与文件相关的方法或类型将其保存在本地(服务器上)。

PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"H:\new\web\pdf_filename.pdf", FileMode.Create));

try and see if this work, please make sure your authentication type is allow user to access that folder. 尝试查看是否可以正常工作,请确保您的身份验证类型为允许用户访问该文件夹。 Not everyone map H drive to same network path, 并非所有人都将H驱动器映射到相同的网络路径,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM