Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

asp.net save pdf file to local driv

Posted on 2014-02-09
5
Medium Priority
?
987 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
  • 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 1500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline
Suggested Courses

963 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