Solved

asp.net save pdf file to local driv

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now