Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am exporting datatable into pdf .its working file but i want to save this file in specific folder.

What I have tried:

Response.ContentType = "application/pdf";
           Response.AddHeader("content-disposition", "attachment;filename=E2E_Variance.pdf");
           Response.Cache.SetCacheability(HttpCacheability.NoCache);

           StringWriter stringWriter = new StringWriter();
           HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

           showleavediv.RenderControl(htmlTextWriter);

           StringReader stringReader = new StringReader(stringWriter.ToString());
           Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
           HTMLWorker htmlparser = new HTMLWorker(Doc);
           PdfWriter.GetInstance(Doc, Response.OutputStream);

           MemoryStream ms = new MemoryStream();
           PdfWriter.GetInstance(Doc, ms);
           //System.IO.File.WriteAllText(Server.MapPath("~/E2E/Dash_Emailed_Shot/Test.pdf"), stringWriter.ToString());

           Doc.Open();
           htmlparser.Parse(stringReader);
           System.IO.File.WriteAllText(Server.MapPath("~/test/Test.pdf"), stringWriter.ToString());


           Doc.Close();
           Response.Write(Doc);
           Response.End();
Posted
Updated 29-Jun-17 1:39am

You can't save files on the client machine, all you can do is write them to the response as you're doing and the client then decides if they want to save the file and if so where to save it.
 
Share this answer
 
Like mentioned in Solution 1, you cannot force the client machine to download anything, and the browser won't let your application to do that either. However, instead of "application/pdf" you can set the content type to octet-stream to force the Save File dialog and it will ask the user to save the file.
C#
// I used the following code to download the file directly.
// Works with System.Web namespace being called
@{
    var file = Server.MapPath("~/documents/" + fileName);
    Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
    Response.ContentType = "application/octet-stream";
    Response.TransmitFile(file);
}

In the above method, I simply tried to pass the file down the stream, you can pass your own file that you just generated perhaps. A good method would be to actually store the file somewhere, and then use that filename (Guid, maybe) and then stream that file down to the users.

Remember: even with this method, user is the king, he has the final decision.

HttpResponse.TransmitFile Method (String) (System.Web)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900