I have this code in the page_load method of an ASP.NET .aspx page that downloads a particular file from the same web site:
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + newFileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(filePath);
Response.Flush();
Response.End();
}
I only want to allow a download if it comes from a clickable link on any page from my own second domain xx.com. All other download attempts coming from other web sites or a direct download need to be disallowed.
How can I detect these download attempts in page_load and allow or disallow the download?
Related
In my ASP.NET Web Forms application, I have to download a string as a file right after i'm doing an action on the web page. The flow is as follows:
Do initial action (button click)
It opens up a popup window where the username and password are required.
Fill in the username and password
Do second action (button click inside popup)
Now, after the second action is done, I want my page to reload and start downloading my string as a file.
If I write my string in the response like below, the file gets downloaded, but the pop-up window does not close.
Response.Write(mystring);
I tried these ways:
Context.Response.Clear();
Response.AddHeader("Content-Disposition", "inline; filename=Warrants.warrant");
Response.ContentType = "application/lsw_print";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(mystring);
Response.Flush();
Response.End();
2.
Context.Response.Clear();
Response.AddHeader("Content-Disposition", "inline; filename=Warrants.warrant");
Response.ContentType = "application/lsw_print";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(mystring);
Response.Flush();
ApplicationInstance.CompleteRequest();
Context.Response.Clear();
Response.AddHeader("Content-Disposition", "inline; filename=Warrants.warrant");
Response.ContentType = "application/lsw_print";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(mystring);
All these versions download my file, but ends the requests and my pop-up doesn't get closed.
Also, the popup I use is a control having an iframe in it.
Thanks!
I try to download a docx file from serverside.
What is my wrong ?
this is code :
FileInfo file = new FileInfo(filepath);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition", "attachment; filename = " + ((Button)sender).CommandName + ".docx");
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.Flush();
Response.Close();
Response.End();
I have posted something similar in another question for an PDF but here goes. It's much easier to stream this sort of data back through an ASHX handler.
Something like what I posted in this question but with a docx file.
Display PDF in iframe
It looks like you are using a normal ASP.NET page and are trying to modify the standard behavior by clearing out the headers, etc. You won't have to fiddle with the headers or anything like that with an ashx handler.
I am writing a webapplication in ASP.net.
I am trying to make a file dialog box appear for downloading something off the server.
I have the appropriate file data stored in a variable called file.
File has fields:
FileType - The MIMEType of the file
FilePath - The server-side file path
Here's the code so far:
Response.Clear();
Response.ContentType = file.FileType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + GetFileName(file));
Response.TransmitFile(file.FilePath) ;
Response.End();
GetFileName is a function that gets me the filename from an attachment object. I only store the path.
The above code is in a function called "Download_Clicked" that is an event that triggers on click. The event is mapped to a LinkButton.
The problem is that when I run the above code, nothing happens. The standard dialog box does not appear.
I have attempted the standard trouble-shooting such as making sure the file exists, and ensuring the path is correct. They are both dead on the mark.
My guess is that because my machine is also the server, it may not be processing properly somehow.
Thanks in advance.
Edit 1: Attempted putting control onto another page, works fine.
Edit 2: Resolved issue by removing control from AJAX Update Panel.
I've found another to do this without removing the update panel. Place the code below in your page load and you'll now be able to use that button to trigger a download.
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button);
Use Response.WriteFile() instead.
Also, don't use Response.End()! This aborts the thread. Use Response.Flush(); Response.Close();
Try changing
Response.AppendHeader("Content-Disposition", "attachment; filename=" + GetFileName(file));
To
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(GetFileName(file))));
If that doesn't work, you can always use Response.BinaryWrite or Resonse.Write to stream the file to the web browser
Here is how transmit the file using Response.Write or Response.BinaryWrite. Put these functions in a library somewhere then call them as needed
public void SendFileToBrowser(String FileName, String MIMEType, String FileData)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = MIMEType;
Response.Buffer = true;
Response.Write(FileData);
Response.End();
}
public void SendFileToBrowser(String FileName, String MIMEType, Byte[] FileData)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = MIMEType;
Response.Buffer = true;
Response.BinaryWrite(FileData);
Response.End();
}
Then somewhere you call these functions like so
SendFileToBrowser("FileName.txt", "text/plain", "Don't try this from an Update Panel. MSAjax does not like it when you mess with the response stream.");
See edit on initial post.
Removed Ajax Update Panel to resolve the error. The panel was stopping the post back to the server.
For more info, see Cris Valenzuela's comment.
Currently, this code works fine in a regular browser window:
if (readerObj.Read())
{
filename = readerObj["TRANATTACHMENTNAME"].ToString();
fileBytes = (byte[])readerObj["TRANATTACHMENT"];
Response.Clear();
Response.ContentType="application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.BinaryWrite(fileBytes);
Response.Flush();
Response.End();
dbConnectorObj.Connection.Close();
dbConnectorObj = null;
return true;
}
Unfortunately, this window needs to be modal (i'm modifying an already existing application). When I run the window modally, there's no file download dialogue.
ASP.NET 2.0
Any thoughts?
I would change the way you are doing this and have the file be server via an HTTP handler. Then you can just link the the handle url passing in the pertinent data to pull correct file or perform authentication and the dialog will pop up regardless.
I have a file, say a PDF on my website and when a user visits a page I want to display a download dialog for the pdf on page load or a button click.
I did a google search and I found two ways to do this but wondering what is the accepted way of doing this? I am currently doing this
string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";
Response.AppendHeader( "content-disposition",
"attachment; filename=" + name );
Response.WriteFile(pdfPath);
Response.End();
(Code was based off code from http://aspalliance.com/259, also found code from
http://www.west-wind.com/weblog/posts/76293.aspx)
Your code, will display the file to the user perfectly. But they will have to use the "Save As" option to actually save it.
If you wish to present the "Save Dialog" to the user, try the following:
string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";
Response.AppendHeader("content-disposition",
"attachment; filename=" + pdfPath );
Response.TransmitFile(pdfPath);
Response.End();
This of course assumes the file actually exists on the server and is not being dynamically generated.
This code will Send Any file to directly on client Browser
Response.ContentType = "application/pdf";
Response.WriteFile(PathToFile);
Response.Flush();