Solved

asp redirect following download

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

12 Experts available now in Live!

Get 1:1 Help Now