0

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. which is typically how people try to download PDF/ZIP in other websites.

My webapp basically allow the user is trying to download the PDF from my server-side code

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

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.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Bryan
  • 8,488
  • 14
  • 52
  • 78
  • Are you downloading in a browser. If, so I think it's up to the browser to specify the path. I guess some browsers offer a "save as" option – TGH Jul 10 '13 at 02:36
  • Define "download" in this case. Is your server-side code "downloading" the file from an off-site service and saving it to a server-side folder? Is a client "downloading" the PDF file from your server-side code? Something else? Please clarify. – David Jul 10 '13 at 02:37
  • http://stackoverflow.com/q/2134392 – Robert Harvey Jul 10 '13 at 02:38
  • I would rather say, the user is trying to download the PDF from my server-side code into their own computer. I have basically uploaded my webapp into Azure and was able to access it online. My pdfbutton basically retrieve data from a specific table, convert and display the data in a pdf format. – Bryan Jul 10 '13 at 02:44
  • @TGH Yes i'm downloading them in a browser. However, there isn't any pop-up asking me where to save my PDF files to. When i click the download button in my webapp, nothing happens as i believe it could be due to my hard-coded file directory. – Bryan Jul 10 '13 at 02:45
  • @TeoChuenWeiBryan: `"the user is trying to download the PDF from my server-side code"` - In that case your code can't specify a client-side path. It's up to the user/browser/client/etc. to decide where to save the file. Keep in mind that your server-side code has no idea what the client-side file system looks like. For example, if you were able to specify the client-side path as "C:\Files\PDFs\" then that wouldn't mean _anything_ on my Mac/Tablet/Phone/Linux Server/etc. – David Jul 10 '13 at 02:47
  • You're right. However, i somehow wasn't able to allow my browser to pop-up a function where it prompt a "save as" and "save to" in the browser like a typical PDF/ZIP folder downloading function – Bryan Jul 10 '13 at 02:54
  • Your code sample have nothing to do with returning document stream in the response to the browser - you either showing wrong code OR have no idea what needs to be done. In later case following search terms should help: "C# content-disposition header" and give you good starting point like http://stackoverflow.com/questions/3889521/response-addheadercontent-disposition-not-opening-file-in-ie6. – Alexei Levenkov Jul 10 '13 at 03:16
  • I have tried this content-disposition header with some error in my another question – Bryan Jul 12 '13 at 01:02

2 Answers2

0

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.

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

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

Avitus
  • 15,640
  • 6
  • 43
  • 53
  • the thing is that i kept trying using method such as server.mappath, or directory.createdirectory into my PDF file creation code but i keep receiving error. I was hoping that the browser would rather pop-up a "save as" and "save to" function while i clicked the download button – Bryan Jul 10 '13 at 02:48
  • As Avitus points out, if you're trying to save this PDF from a browser, you're at the mercy of whatever dialog the browser displays. – Robert Harvey Jul 10 '13 at 02:55
  • However, there isn't any dialog displayed by my browser. In the first place, i have used a hard-coded directory which is wrong when published online – Bryan Jul 10 '13 at 03:04
0
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. Change it from the Session variable to whatever you want.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
prospector
  • 3,389
  • 1
  • 23
  • 40