Solved

asp.net save pdf file to local driv

Posted on 2014-02-09
5
946 Views
Last Modified: 2014-02-12
trying to search for code snippets for saving a created pdf file in server (Script/doc1.pdf) to local drive location (c:\pdffolder)

have sought of the savefiledialog method, but it only applied on winform, trying to find asp.net method
0
Comment
Question by:doramail05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39846407
I assume by local drive you mean browser side (as opposed to server side)?

If so you have no control over that at all. You can send the attachment back to the user such that it opens automatically or they are prompted to save it. But where they save it is up to them.

To automatically open an attachment:

Response.AppendHeader("Content-Disposition", "inline; filename=doc1.pdf");

Open in new window


To prompt to save:

Response.AppendHeader("Content-Disposition", "attachment; filename=doc1.pdf");

Open in new window

0
 
LVL 1

Author Comment

by:doramail05
ID: 39846409
how about if i would like to set the location of the local drive?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39846454
As I said "If so you have no control over that at all" :)
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 39848505
I would use Response.BinaryWrite()
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.binarywrite(v=vs.110).aspx

First, the file needs to be a byte array:

byte[] myPDFBytes = <some byte array representing the PDF>;
Response.Clear();
Response.ContentType = "binary/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename="\" + filename + "\"");
Response.BinaryWrite(myPDFBytes);
Response.Flush();
Response.End();

Open in new window

0
 
LVL 1

Author Comment

by:doramail05
ID: 39852701
found this,

protected void Page_Load(object sender, EventArgs e)
{  

    FileStream fs = File.OpenRead(Server.MapPath("~/imgName.jpg"));
    byte[] buffer = new byte[(int)fs.Length];
    fs.Read(buffer, 0, (int)fs.Length);
    fs.Close();
    SetResponse("imgName");
    HttpContext.Current.Response.BinaryWrite(buffer);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Close();
}

private static void SetResponse(string fileName)
{
    string attachment = "attachment; filename=" + fileName + ".jpg";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.AddHeader("content-disposition", attachment);
    HttpContext.Current.Response.ContentType = "image/jpeg";
    HttpContext.Current.Response.AddHeader("Pragma", "public");
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 42
asp.net get gridview datakey from row command 2 45
disadvantage of code behind appoarch in asp.net 3 48
CSS question 16 62
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question