简体   繁体   English

下载PDF时如何自动创建文件夹/在浏览器中运行“另存为”和“另存为”功能

[英]How to automatically create a folder / run a “save as” and “save to” function in browser when downloading PDF

Currently I'm trying to find a way to automatically create a new folder to save a PDF into it or either save it in a common file all computers have or make my browser pop-up run a "save as" and "save to" function whenever the client click the download button. 目前,我正在尝试寻找一种自动创建新文件夹的方法,以将PDF保存到其中,或者将其保存在所有计算机都拥有的通用文件中,或者使我的浏览器弹出窗口运行“另存为”和“另存为”当客户端单击下载按钮时,该功能将起作用。 which is typically how people try to download PDF/ZIP in other websites. 人们通常会尝试在其他网站上下载PDF / ZIP。

My webapp basically allow the user is trying to download the PDF from my server-side code 我的webapp基本上允许用户尝试从我的服务器端代码下载PDF

As you can see from my codes, I have been trying ways to save my PDF file and this is how i try to save my PDF in a hard-coded directory like this 从代码中可以看到,我一直在尝试保存PDF文件的方法,这就是我尝试将PDF保存在这样的硬编码目录中的方式

var doc1 = new Document();
var filename = "Official Report" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
var output = new FileStream(Path.Combine("C:/Users/apr13mpsip/Downloads", filename), FileMode.Create);
iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);

How do i use the 我该如何使用

server.mappath("");

or how do i automatically create a new folder when trying to download the PDF file. 或尝试下载PDF文件时如何自动创建新文件夹。

If you are using a web application you won't be able to create a folder through a browser on the clients computer because that would be a security issue. 如果您使用的是Web应用程序,则将无法通过客户端计算机上的浏览器创建文件夹,因为这将是一个安全问题。

If you are doing this in another type of application that is on the user's computer. 如果您正在用户计算机上的另一种类型的应用程序中执行此操作。 Then all you have to do is include System.IO and do a Directory.CreateDirectory 然后,您要做的就是包含System.IO并执行Directory.CreateDirectory

http://msdn.microsoft.com/en-us/library/54a0at6s(v=vs.100).aspx http://msdn.microsoft.com/zh-CN/library/54a0at6s(v=vs.100).aspx

string pid = Session["PatientID"].ToString();
string strUploadPath = Server.MapPath("Files") + "\\" + pid + "\\";
if (!System.IO.Directory.Exists(strUploadPath))
{
    System.IO.Directory.CreateDirectory(strUploadPath);
}
fuSample.SaveAs(strUploadPath + fuSample.FileName);
lblMessage.Text = "File Successfully Uploaded";

This creates a subfolder in a files folder, the subfolder can be the user's ID so they are all unique. 这将在文件文件夹中创建一个子文件夹,该子文件夹可以是用户的ID,因此它们都是唯一的。 Change it from the Session variable to whatever you want. 将其从Session变量更改为所需的值。

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

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