0

I created a PDF webapp where users are able to generate various type of PDF on both the computer and mobile phone. However, i run my program on a localhost and this is how i save my PDF based on my computer's file directory

var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);

However, when i publish my webapp onto azure, i wasn't able to download from both my computer and mobile phone. Therefore i believe that it could be due to my default file directory.

Hence i would like to ask how to do a default file directory for all computer and mobile phone?

Or could it be i left out something that is necessary when the webapp is published online

Thanks.

PS : I hardcoded a default file path in order for me to test my application on a localhost to ensure a perfect working condition. Therefore i'm finding a way to find a default common file directory for all mobile/computer users when they attempt to download the PDF instead of my usual hard-coded file path

UPDATE

I tried using the method Server.MapPath but receive some error.

var doc1 = new Document();
        var filename = Server.MapPath("~/pdf") + "MyTestPDF" + 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);

        using (var output = File.Create(filename))
        {
            iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);
        }


        doc1.Open();

This is the error i received

ObjectDisposedException was unhandled by user code
Cannot access a closed file.
Bryan
  • 8,488
  • 14
  • 52
  • 78

1 Answers1

1

When you write a Web Application you shall never use hard coded paths, and the last place where you should save files is C:\Users !! It does not matter whether this is Azure or not. It is general rule for any kind of web applications!

In your case I suggest that you create a folder within your application named pdf or something like that and save files there with the following code:

var fileName = Server.MapPath("~/pdf") + filename;
using (var output = File.Create(fileName) )
{
 // do what you want with that stream
 // usually generate the file and send to the end user
}

However there is even more efficient way. Use the Response.OutputStream and write the resulted PDF directly to the response. Will save you a lot of space on the local server, and the logic to delete unused generated files.

astaykov
  • 30,768
  • 3
  • 70
  • 86
  • I have updated my question on why i have initially used a hard-coded file path and i also attempted your method "server.mappath" – Bryan Jul 09 '13 at 09:29
  • why do you want to open doc1? Please refer to [this SO question](http://stackoverflow.com/questions/1196059/itextsharp-sending-in-memory-pdf-in-an-email-attachment) and its answer to get to know how to handle PDF creation with iTextSharp! As your error now does not relate in any way with Azure. Secondly, I highly recommend that you write your PDF directly to the response stream, unless you want to save it in a persistent storage. And local file system in Azure Web Role is not a Persistent Storage. – astaykov Jul 09 '13 at 12:17
  • Actually I dont have the intention of storing my PDF into azure. My PDF function actually just want to generate a simple file by retrieving data from a specific table and then be brought into PDF format. Hence, i actually just need a common way directory that can help to save my PDF on both phone and computer rather than saving the PDF into azure. – Bryan Jul 10 '13 at 01:35