Solved

asp.net save pdf file to local driv

Posted on 2014-02-09
5
955 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

630 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