Solved

asp redirect following download

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

717 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