0

I have this function made to save a PDF file in the local disk.

How do I automatically close the window after the download completed with an alert/msg box stating that the window will close?

This doesn't work:

Response.Write ("<script>alert('Save Finished'); self.close();</script>")

TheCode:

Private Function SaveLocal1(ByRef cnCon, ByVal PDFFile)
    Dim ObjStream
    Dim ObjFileSys
    Dim Size
    Dim fpath
    Dim bdata

    'Response.Write("<script>alert('local');</script>")

    fpath = "C:\1.pdf" 'path of the file in the local server
    filename = "LocalSave.pdf"     'name to be saved
        'Response.Write("<script>alert('file paths');</script>")

    Set ObjStream = Server.CreateObject("ADODB.Stream")
    ObjStream.Open
    ObjStream.Type = 1
    ObjStream.LoadFromFile fpath
        'Response.Write("<script>alert('server.createobject');</script>")

    Response.buffer = TRUE
    Response.AddHeader "Content-Disposition","attachment;filename="& filename & ";"
    Response.ContentType = "application/pdf"

    Set ObjFileSys =  CreateObject("Scripting.FileSystemObject")
    Size = ObjFileSys.GetFile(fpath).Size

    bdata = ObjStream.Read(Size)

    Response.BinaryWrite(bdata)     'the file will be saved in the downloads folder

    Response.Write("<script>alert('Save to local finish');</script>")
    ObjStream.Close
    Set ObjStream = Nothing    

    Response.Flush
    Response.End
    Response.Write ("<script>alert('Save Finished'); self.close();</script>")
End Function
Aditya
  • 2,876
  • 4
  • 34
  • 30
Krowned
  • 1
  • 1
  • 4
  • did you have a look at this topic: http://stackoverflow.com/questions/57854/how-can-i-close-a-browser-window-without-receiving-the-do-you-want-to-close-thi, it describes how to close a window via Javascript. – kloarubeek Jan 10 '14 at 10:06

2 Answers2

3

I suppose there are could be 2 issue in your code.

2nd issue:

Response.End
Response.Write ("<script>alert('Save Finished'); self.close();</script>")

Response.End -- is last command. That is all. "Document" completed.

The End method causes the Web server to stop processing the script and return the current result. The remaining contents of the file are not processed.

Change order of following comand to

Response.Write ("<script>alert('Save Finished'); self.close();</script>")
Response.End
Zam
  • 2,880
  • 1
  • 18
  • 33
0

Use response.redirect and send this page to another page, use window.close on body onload