Solved

asp redirect following download

Posted on 2010-08-23
4
503 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
  • 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now