PDA

View Full Version : Problems with forced download - script


olduhu
10-01-2002, 01:54 PM
I needed a script to force downloads of files like *.doc, *.jpeg, etc... instead of opening them automatically in the browser window. Well, I have found a script that does the job quite well - first you are asked whether to open or save the document and then you can specify where to save the file to.
However, if the user cancels the second dialog box (the Save as... box), the browser hangs up. You then have to close the browser, disconnect and reconnect to make this site work again. The reason for this is that although the user cancelled the file-transfer the connection stays open.
Now my question:
Could anyone please tell me how to avoid this malfunction? I know that I have to somehow check whether the "Cancel"-Button was pressed in the second dialog, or at least I could imagine it to work that way... ;-) But how do I do this?

Here's the script I've used so far:

<%
Response.Buffer = TRUE
Response.Clear
Response.Expires = 0
Response.ContentType = "application/msword"

strFileName = "test.doc"
strMyPath = Server.MapPath("./docs/") & "\" & strFileName

Response.AddHeader "Content-Disposition","attachment;filename=" & strFileName

Set objFileSys = Server.CreateObject("Scripting.Filesystemobject")

Set objFile = objFileSys.GetFile(strMyPath)
FileSize = objFile.Size
Set objFile = Nothing

Set objFile = objFileSys.OpenTextFile(strMyPath, 1, false, -1)

Response.BinaryWrite(objFile.Read(FileSize))

Set objFile = Nothing

Set objFileSys = Nothing

Response.Flush
%>

Thanks for your help!!! I'd really appreciate it...

Alekz
10-01-2002, 02:01 PM
Hi,
I'm not sure about this, but probably You can write the file in chunks...
Bin = objFile.Read(FileSize)
Response.BinaryWrite(Bin(1))
isCientConnected -> not sure about the syntax of this one...
If it still connected write the rest...
Just a thought...

Alex

whammy
10-02-2002, 01:32 AM
You can do this with ASP by specifying the type of information you are transmitting - (which it appears you are experimenting with) but the simplest solution is to just save the download file as a compressed .zip file.

That will always force a user to download it. And it saves you time! :)

Besides, if someone doesn't know how to unzip a file, they need to learn. :D

glenngv
10-02-2002, 06:33 AM
i run your code in my server and it did not hang when i chose cancel in the 2nd dialog box.
im using IIS4 and IE5.5

olduhu
10-02-2002, 12:22 PM
alright... tested it on a different server now and it didn't cause a hangup, so the problem isn't the script but my server-configuration!
but that seems to be a different story :-(
thanks @glenngv...
and thanks to everybody for your help!