Bryan
Bryan

Reputation: 8697

Specify the location to save the pdf file asp.net

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;
    }

Upvotes: 1

Views: 14933

Answers (2)

Sanyami Vaidya
Sanyami Vaidya

Reputation: 809

Try this

   public string CommonFileSave(HttpPostedFileBase postedFile, string filePath)       
   {
        string resultResponse = "sccuess";

        if (!Directory.Exists(filePath))
        {
            Directory.CreateDirectory(filePath);
            postedFile.SaveAs(filePath + postedFile.FileName);
        }
        else
        {
            filePath = filePath + postedFile.FileName;
            if (!System.IO.File.Exists(filePath))
            {
                postedFile.SaveAs(filePath);
            }
        }
        return resultResponse;
    }

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100555

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.

Upvotes: 3

Related Questions