Solved

asp.net save pdf file to local driv

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

10 Experts available now in Live!

Get 1:1 Help Now