Jump to content

Handling File Download


Recommended Posts

Hi there

I have IE web application and I have a drop down list (Export type - PDF, EXcel, CSV) and export link. I've managed to work on these 2 controls. Now when you click the Export button link, it loads a separate page and then automatically load a small standard file download window - OPEN, SAVE, CANCEL. Obviously, when you click SAVE button, it will lauch SAVE AS (windows standard save as) and then you type in the File Name.

How do I interact with this window? I want to click the Save button and then point the file name to save to specific location?

Here's code that I am working on and it's quite interesting that the code stop working after click the Export button and and I can see the open up another page in the background but instead of come up "File Download" window. It shut and then scripting completed ?!?!

Any ideas? I am not sure how to debug this? Security reason perhaps?

$oForm2 = _IEFormGetCollection($oIE, 0)

$o_FormatReport = _IEFormElementGetObjByName  ($oForm2, "ctl00_ContentPlaceHolder1_rprtvwrGraph_ReportToolbar_ExportGr_FormatList_DropDownList")

_IEFormElementOptionselect ($o_FormatReport, "PDF", 1, "byValue", 1)

Sleep(2000)

_IELinkClickByText($oForm2, "Export")

Sleep(3000)

WinWaitActive("File Download")
WinActivate("File Download")

WinWaitActive("File Download - Security Warning")
WinActivate("File Download")

If @OSVersion = "Win_VISTA" Then ControlClick("File Download", "", "[CLASS:Button; INSTANCE:1]")
If @OSVersion = "Win_XP" Then ControlClick("File Download", "", "[CLASS:Button; INSTANCE:2]")


WinWaitActive("Save As")
WinActivate("Save As")
ControlSetText("Save As", "", "[CLASS:Edit; INSTANCE:1]", "c:\fakefile123.xls" )
If @OSVersion = "Win_VISTA" Then ControlClick("Save As", "", "[CLASS:Button; INSTANCE:1]")
If @OSVersion = "Win_XP" Then ControlClick("Save As", "", "[CLASS:Button; INSTANCE:2]")
$i = 1
; Check to see if the file was downloaded
While 1
    If FileExists("C:\fakefile123.pdf") Then ExitLoop
    Sleep(1000)
    $i = $i + 1
    If $i > 240 Then
        MsgBox(0, "", "File not downloaded!")
        _IEQuit($oIE)
        Exit
    EndIf
WEnd
_IEQuit($oIE)
msgbox(0, "", "Success")

I am appreciated your feedback.

Thanks

Edited by DCAlliances
Link to comment
Share on other sites

Hi! I stopped with the same problem. When I clicked the button on some page with params, site gives to browser a file according to params and File Download popups (or not popups). Same problem was discussed here half year ago. But the only solution was clicking the buttons of File Download dialog window.

Your script completes because you working with one instance on IE and File Download popups in another instance of IE.

I spent some time to find solution on how to handle File Download and thats what I found. Besides the methods and properties Internet Explorer object have also some events. For our case there is FileDownload event, which fires before FileDownload window popups. With this event window can be canceled and generated direct URL can be read from IE. Looks like Download Managers uses same routine.

I tried to write a sample code in VBA (excel) but looks like it can't handle global objects' external events. Here is the link to MSDN description on IE object and its childs.

Link to comment
Share on other sites

  • 2 weeks later...

*bump*

Having a similar issue, I think using _IEHeadInsertEventScript is what I would need, but not sure how to script something to monitor the FileDownload event, and when it is fired capture the URL to a variable, pass it back to autoit, and cancel the download. Once its back in autoit I plan on either downloading nativly or opening directly in excel depending on which works easier.

Can't wait until this final step of getting the file is done, no more having to script against this site wrote almost entirely off javascript.

If it was a simple link on a page I could easily put in variable, but the site uses javascript to dynamically create a filename for each download attempt.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...