Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect after file download operation

Status
Not open for further replies.

Foamcow

Programmer
Nov 14, 2002
6,092
GB
Hi,
I'm fairly inexperienced with ASP and I'm wondering if there is a way to make a page redirect after a file download has taken place.

This is part of a system that restricts access to some files and tracks the number of downloads of a particular file.

The access control and downloading works fine but once the download is complete I want to refresh the page to show a success message etc.

Response.Write and Response.Redirect do not seem to have any effect.

I thought the issue might be that the Response buffer is 'true' but even if I try to Response.Write or Response.redirect and then flush the buffer nothing happens.

Code:
<!-- #include virtual="/data/functions.asp" -->
<%
Response.Expires = -1000 'Makes the browser not cache this page
Response.Buffer = True 'Buffers the content so our Response.Redirect will work


If Session("UserLoggedIn") <> "true" Then
	Session("FileId")=Request.QueryString("fid")
    Response.Redirect("login.asp?fid=") & Request.QueryString("fid")
ElseIf Not IsNumeric(Request.QueryString("fid")) Then
	fail
Else

	'download the file
	fileId = Request.QueryString("fid")
	Response.Write("<p>File id = " & fileId & "</p>")

	'lookup the file url in the database from the fileId
	open_database_connection()
	sql= "SELECT url FROM files WHERE id = " & fileId & " AND published=true"
	Set oRS = Server.CreateObject("ADODB.RecordSet")
	Set oRS = connDatabase.execute(sql)

	'if found then download the file
	If Not oRS.EOF Then
		Response.Write("<p>Downloading</p>")
		Response.Write("<p><a href=""#"">Close this window</a></p>")
		DownloadFile("/downloads/"&oRS("url"))	
		'Write out a success message or redirect to another page - this bit doesn't work, the script seems to stop here.
		Response.Redirect("[URL unfurl="true"]http://10.0.0.116/")[/URL]
	
	Else
		'if not found then show an error message
		fail
	End If

End If



Sub fail 
	Response.Write("<p>Sorry, that file is not currently available for download.</p>")
End Sub

Sub DownloadFile(sFile)
  strAbsFile = Server.MapPath(sFile) 
   '-- create FSO object to check if file exists and get properties  
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")  
	 If objFSO.FileExists(strAbsFile) Then  
		Set objFile = objFSO.GetFile(strAbsFile)
		ContentType = "application/x-msdownload"
		Response.Buffer = True
		Const adTypeBinary = 1
		Response.Clear
		Set objStream = Server.CreateObject("ADODB.Stream")
		objStream.Open
		objStream.Type = adTypeBinary
		objStream.LoadFromFile objFile.Path
		ContentType = "application/octet-stream"
		Response.AddHeader "Content-Disposition", "attachment; filename=""" & objFile.Name & """"
		Response.Charset = "UTF-8"
		Response.ContentType = ContentType
		Response.BinaryWrite objStream.Read
		Response.Flush
		objStream.Close
		Set objStream = Nothing	
	End If	
 End Sub
%>

<honk>*:O)</honk>

Earl & Thompson Marketing - Marketing Agency Services in Gloucestershire
 
From memory once you do Flush you can't redirect sense the headers are written.

Try sticking the Redirect rate before the Response.Flush. I did a client script once when I had the same problem. I'll see if I can recreate it for you and post back when I get to work if someone else hasn't

[sub]____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done[/sub]
 
Nope, forget that idea.

I think the issue is only an issue because the file download is being triggered from with a kind of popup window created using Greybox (it's actually a <div> containnig an iframe that loads a page so it appears to float over the main page).

If I get rid of that then the file open action will just behave as normal. Thinking about it, this may be better anyway.

<honk>*:O)</honk>

Earl & Thompson Marketing - Marketing Agency Services in Gloucestershire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top