指定位置保存PDF文件asp.net [英] Specify the location to save the pdf file asp.net

查看:246
本文介绍了指定位置保存PDF文件asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code生成一个样本的PDF文件。然而Server.MapPath方法保存在项目文件夹中的文件。我如何让PDF文件被保存在我自己的桌面?

 保护无效btnPDF_Click(对象发件人,EventArgs的)
    {


        VAR文件=新的文件(PageSize.A4,50,50,25,25);
        VAR文件名= DDLCase.SelectedItem.Text +.PDF;
        无功输出=新的FileStream(使用Server.Mappath(文件名),FileMode.Create);
        VAR作家= PdfWriter.GetInstance(文件输出);
        document.Open();
        VAR welcomeParagraph =新段(测试1);
        document.Add(welcomeParagraph);
        document.Close();
        btnP​​DF.Enabled = FALSE;
    }
 

解决方案

这是很清楚你的问题是什么,它​​应该是pretty的简单的替换使用Server.Mappath(文件名)与其他一些位置。

一个有用的功能是 Path.Combine 这样你就可以正确地构建文件路径:

 无功输出=新的FileStream(Path.Combine(C:\\ myPDF \\,文件名),FileMode.Create);
 

需要注意的是做正确的服务器文件夹在计划存储文件必须有足够的权限,以允许ASP.Net过程中保存文件存在。如果您使用的是Windows身份验证与模拟过程中请求下运行它变得棘手的帐户code将传入的用户的帐户。

Below is my code which generates a sample PDF file. However the server.mappath method saves the file at the project folder. How do i allow the PDF file to be saved in my own desktop?

protected void btnPDF_Click(object sender, EventArgs e)
    {


        var document = new Document(PageSize.A4, 50, 50, 25, 25);
        var filename = DDLCase.SelectedItem.Text + ".pdf";
        var output = new FileStream(Server.MapPath(filename), FileMode.Create);
        var writer = PdfWriter.GetInstance(document, output);
        document.Open();
        var welcomeParagraph = new Paragraph("Test1");
        document.Add(welcomeParagraph);
        document.Close();
        btnPDF.Enabled= false;
    }

解决方案

It is very unclear what your problem is as it should be pretty straightforward to replace Server.MapPath(filename) with some other location.

One useful function is Path.Combine so you can correctly build path to a file:

   var output = new FileStream(Path.Combine("c:\\myPDF\\", filename), FileMode.Create);

Note that to be done properly folder on server where you plan to store files must have enough permissions to allow ASP.Net process to save files there. If you use Windows auth with impersonation it becomes trickier as account code is running under during request will be incoming user's account.

这篇关于指定位置保存PDF文件asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆