Solved

asp redirect following download

Posted on 2010-08-23
4
505 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 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