I'm trying to get a PDF page that is within the context of my website to display when a user designates. I have the following code:
if (context.User.Identity.IsAuthenticated)
{
string SampleURL = context.Request.CurrentExecutionFilePath; //CurrentExecutionFilePath;
context.Response.Buffer = true;
context.Response.Clear();
using (FileStream fs = new FileStream(SampleURL,FileMode.Open)) //System.IO.File.OpenRead(path))
{
int length = (int)fs.Length;
byte[] buffer;
using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
context.Response.Clear();
context.Response.Buffer = true;
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(buffer);
context.Response.End();
}
}
else
{
context.Response.Redirect(
"~/Error/invalid_access.aspx");
}
The only problem is I cannot get the PATH to work correctly. If I was to call the PDF directly thru the URL it would be http://www.abc.com/reports/sample.pdf but I cannot get myself back to that location. I implemented an HTTPHandler to prevent someone from going to the URL but now I need to stream the file back to a browser and write it.
thoughts, comments, suggestions?
edit: Its the PATH that I cannot get the relative url to point to the proper location. context.Request.CurrentExecutionFilePath gives me "/www.abc.com/sample_reports/sample.pdf" but I just can't seem to twist that around to be able to open/read it
What do you mean by path? Name of the file? You can do that with Content-Disposition header.
Response.Addheader "Content-Disposition", "attachment;Filename=WhateverName.pdf"
You have to open it from the local server. If it is located on your server you can do
FileStream fs = new FileStream(Server.MapPath("/example.pdf"),FileMode.Open)
If you want to load it from an URL you have to download the PDF first.
WebClient client = new WebClient ();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead (args[0]);
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();`
I would change a few things
First I might consider switching the file reading to something easier if it's a file on disk
byte[] buffer = File.ReadAllBytes( path );
now for the context
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.Buffer = true;
context.Response.ContentType = "application/pdf";
context.Response.AddHeader( "content-disposition","attachment; filename=file.pdf" );
context.Response.AddHeader("Content-Length", buffer .Length.ToString() );
context.Response.BinaryWrite(buffer);
context.Response.Close();
context.Response.End();
context.Response.Flush();
Is the above code overkill? Possibly but I've found with all the different browsers running around overkill is worth it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.