473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Response.WriteFile & Response.Redirect

The Human Tangent
I'm publishing a page URL
eg mysite.com/disclaimer/someinfo
this page shows an agreement, and if the user clicks the agree button then a PDF file is streamed as an attachment.

The purpose of this is the user can then share this URL but anyone else following the link must also read the agreement before downloading the file. The actual file does not have a URL but is held elsewhere on the server so the only way to download the file is if you click the "[Agree]" button.

However - what I am finding difficult is that after clicking the button and starting the download I'd like the page to "go away". The page with the buttons stays on the screen. Ideally they'd be redirected back to the page they came from. I'm keen to not require javascript or cookies. The referrer is stored in the initial page load to a hidden field on the form.

I've tried different approaches, attempting to write a 307 redirect in the header with the file brought up warnings to the user that their post data was being redirected. The code I have below only works in browsers that support meta refresh (eg Firefox; IE7 not Chrome/Safari)

I can see the logic of why this doesn't work. There's one response to the browser and that is the file as expected. What would be the correct way to send them the file and then redirect the browser to another page. It feels from a users p.o.v. that this "Accept" page should disappear once they've clicked the button.

I have:

Expand|Select|Wrap|Line Numbers
  1.         protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             if (Request.UrlReferrer != null & Request.UrlReferrer.ToString() != "")
  4.             {
  5.                 Uri referrer = Request.UrlReferrer;
  6.                 if (!IsPostBack)
  7.                 { hfReferrer.Value = Request.UrlReferrer.ToString(); }
  8.             }
  9.         }
  10.  
  11.         protected void buttonAccept_Click(object sender, EventArgs e)
  12.         {
  13.             //stream file
  14.             Node nodeCurrent = Node.GetCurrent();
  15.             string fileName = nodeCurrent.GetProperty("documentFile").Value.ToString();
  16.             System.IO.FileInfo file = new System.IO.FileInfo(fileName); 
  17.             if (file.Exists) //set appropriate headers  
  18.             {
  19.                 Response.Clear();
  20.  
  21.                 if (hfReferrer.Value !="")
  22.                 {Response.AddHeader("Refresh", string.Format("3; URL={0}", hfReferrer.Value));}
  23.  
  24.                 Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  25.                 Response.AddHeader("Content-Length", file.Length.ToString());
  26.                 Response.ContentType = "application/pdf";
  27.  
  28.                 // write file to browser
  29.                 Response.WriteFile(file.FullName);
  30.                 Response.End();
  31.             }
  32.  
  33.  
  34.         }
  35.  
I'd prefer not to need to use javascript/pop-ups etc.
Presently the user just has to click a link to go to another page, but this doesn't feel quite right.

Ideally I'd like to follow a good standard for this. But I am not sure where to look.
Jan 31 '12 #1
0 1782

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jim Flath | last post by:
I have a situation when I need to set the response status code to a non 200 and then serve up a generic error page (html page not a jsp). I can successfully call response.setStatus(...
0
by: Earl Teigrob | last post by:
I have a datagrid with a download button that raises the itemcommand event. In the event handler for this event, I have the code below. The problem is that the download code requires a...
2
by: Carter | last post by:
i have a treeview on a webform (.aspx). when the user selects an appropriate node on the tvw. and clicks on a link button, i'm downloading a corresponding file to the client (from the server). so far...
0
by: ProJee | last post by:
Hi, Response.WriteFile (or Response.OutputStream.Write) finishes immediately, not after the file is completely downloaded. It finishes before (!) the user clicks the "Save" or "Open" browser...
2
by: David Union | last post by:
Hi. I'm posting this here because I don't know exactly what the best group is. This is for an aspx page with Visual Basic as the code-behind page. I am doing very simple code... in the middle...
8
by: Scott C. Reynolds | last post by:
I want to serve a PDF right to a web page (cannot link browser directly to PDF file). Stumbled across Response.WriteFile this morning. On my machine (XP Pro) this worked fine: private void...
4
by: david | last post by:
I has a question: I can use Response.WriteFile to display images such as .jpg. But I can not us it to display words doc file in EI by calling Response.WriteFile("testdoc/DownloadLarge.doc"). It...
1
by: ad | last post by:
I want to download a zip (c:\test\mine.zip) in the server to client. I modified the code form http://support.microsoft.com/default.aspx?scid=KB;EN-US;q306654 and write it to a click event of a...
3
by: MJP | last post by:
I have a button which kicks off the generation of a report after which the file will be downloaded. The report generation can take a long time, so client side onclick event of the button also...
5
by: Khafancoder | last post by:
Hi guys, i am building a FileSharing website, i wanna allow users to be able using download managers such as DAP to download files from webserver but don't allow them to retrive files url......
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.