?
Solved

asp redirect following download

Posted on 2010-08-23
4
Medium Priority
?
517 Views
Last Modified: 2012-05-10
I have a classic asp page that creates a file on the server. This file needs to be downloaded to my client's pc and then have a new webpage shown. My script creates the file, the file is download using the following function, but I can not get any type of redirect to work. I have tried this same download code in a separate file, adding the redirect in the calling and called file and nothing works. After the download completes, the page remains unchanged.

Sub DownloadFile(sFile)
    strAbsFile = Server.MapPath("\TimeMag")  & "\" & sFile
    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

Open in new window

0
Comment
Question by:harpsw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:RRRRyan
ID: 33507431
You cannot do what you're attempting because of how the browser handles downloading files. You actually must redirect before the download and then you can redirect TO the download. This will show the page you want them to land on while displaying the file save dialog. When you send them the file it is like going to a new page, once you send the browser your code has no control over it's behavior unless there is a script there. There is no script there because it's a download. You can open a new window for the download and then put an arbitrary timeout for redirect to give the illusion of redirecting after download, but it will only be an illusion, and download speeds vary significantly.
0
 

Author Comment

by:harpsw
ID: 33507581
Thanks RRRRyan. I understand much better but I'm still missing something. The code for the new download page is below. With the 2 forms, the first displays and after the submit, the download occurs but the 2nd form is not shown. I have also tried this with just the 2nd form and the download call and the original calling page shows.

Thanks for the help.

<code>
<%@ Language=VBScript %>

<html>
<body>
<%
      pFile = request.querystring("file")
%>

<% if request.form("SubmitBtn") <> "Download File" then %>

<form method="POST" name="Form2" action="u.asp?file=<%=pFile %>">
<input type="hidden" name="submitted" value "yes">
<div align="center" style="width: 700; height: 364">
      <h1 align="left" <font face="Arial" size="3">Download File</font></h1>

      <input type="submit" name="SubmitBtn" value="Download File" > <br>
</div>
</form>
<% else %>
      <form method="POST" name="Form3" action="http://mysite.com">
      <input type="hidden" name="submitted" value "yes">
      <div align="center" style="width: 700; height: 364">
            <h1 align="left" <font face="Arial" size="3">Go To New Page</font></h1>

            <input type="submit" name="SubmitBtn" value="New Page" > <br>
      </div>
      </form>

      <%
            DownloadFile(pFile)
      %>

<% end if %>

</body>
</html>


<%

Sub DownloadFile(sfile)
  sPath = Server.MapPath("\files")  & "\" & sFile
  sName = sFile
  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 sPath
  ContentType = "application/octet-stream"
  Response.AddHeader "Content-Disposition", "attachment; filename=""" & sName & """"
  Response.Charset = "UTF-8"
  Response.ContentType = ContentType
  Response.BinaryWrite objStream.Read
  Response.Flush
  objStream.Close
  Set objStream = Nothing
End Sub

%>
</code>
0
 
LVL 3

Accepted Solution

by:
RRRRyan earned 2000 total points
ID: 33507643
you may want to just put the actual call to the download on the second page script. So they click to download, but all that does is sends them to a page and that page starts the download. Hope this helps.
0
 

Author Comment

by:harpsw
ID: 33511519
Thanks for the suggestions. I was able to get this to work by placing the download code in a subroutine within the create file script, automatically calling that after the file is created and adding a second submit button to the main form that issues a response.redirect when clicked.  For anyone who is interested, my create file code is:

<code>

<%
      if request.form("SubmitBtn") = "New Page" then
            Response.redirect "http://myothersite.com/"
      end if

      if request.form("SubmitBtn") <> "Create File" then

%>

<form method="POST" name="Form1" action="CreateFile.asp">
<input type="hidden" name="submitted" value "yes">
<div align="center" style="width: 700; height: 364">
      <h1 align="left" <font face="Arial" size="3">Create File</font></h1>

      <input type="submit" name="SubmitBtn" value="Create File" > 
      <input type="submit" name="SubmitBtn" value="New Page " > <br>
</div>
</form>

</body>
</html>

<% else
       '  create the file
      '   call download procedure
   end if
%>


</code>

0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

649 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