views:

755

answers:

6

Doing like so:

Shell ("C:\Program Files\Internet Explorer\iexplore.exe -embedding http://www.websiteurl.com")

Doesn't work how I need it as I essentially need it to be able to redirect and prompt a user to download a file. Any ideas?

+1  A: 

There are a couple of things you could do.

  • Use an external program like wget to get the file instead of IE. You can get wget for free at http://www.cygwin.com with the cygnus tools. It's GPL, so just watch out if you have a commercial product.

  • Write a little .NET program that uses the HttpWebRequest class to get the file and shell out to that program instead of IE. I don't think you're going to have a lot of luck shelling out to IE itself. Sounds like a, to paraphrase Steve Jobs, "bag of hurt".

Dave Markle
Just seemed like there'd be an easy way to open ie and point it towards a link without ie actually becoming visible :( Guess not.
A: 

If all you are trying to do is download a file, you can use URLDownloadToFile.

Glomek
That would work normally, but what I wanted to do is have this hit my a seperate folder on my domain which keeps track of how many times my application is downloaded. So page one has a counter that gets hit and thne redirs to the actual download location.
Hm... in that case, shelling out to something like cURL (http://curl.haxx.se/) may be the easiest thing to do. -o lets you specify where to save the file and -L lets you tell it to follow redirects.
Glomek
Another thought -- You may be able to track how many times it has been downloaded using the web server logs.
Glomek
A: 

The Internet Explorer interface is exposed to ActiveX via the WebBrowser control (contained in %systemroot%\system32\shlwapi.dll). While it may not be very elegant, you could easily place the control somewhere off the visible area of the form.

The control is very simple to use.

smbarbour
A: 

Your best bet is creating separate download application using some .NET http object in order to download the file. I'd recommend WebClient.

If you really gotta stick to VB6, I'm sure you can use some basic socket work in order to download the file directly.

+4  A: 

Internet Explorer exposes a COM accessible interface you can use. If you really have to. I'd recommend against it - its comparatively slow, error-prone, cumbersome and resource-intensive.

What solves your problem more elegantly is using WinHTTPRequest. In your Project, reference "Microsoft WinHTTP Services, version 5.1", and then go on like this:

Dim HttpRequest As New WinHttp.WinHttpRequest
Dim TargetUrl As String
Dim TargetFile As String
Dim FileNum As Integer

TargetFile = "C:\foo.doc"

TargetUrl = "http://www.websiteurl.com"
HttpRequest.Open Method:="GET", Url:=TargetUrl, Async:=False
HttpRequest.Send

If HttpRequest.Status = 302 Then

  TargetUrl = HttpRequest.GetResponseHeader("Location")
  HttpRequest.Open Method:="GET", Url:=TargetUrl, Async:=False
  HttpRequest.Send

  If HttpRequest.Status = "200" Then

    FileNum = FreeFile
    Open TargetFile For Binary As #FileNum
    Put #FileNum, 1, HttpRequest.ResponseBody
    Close FileNum 

    Debug.Print "Successfully witten " & TargetFile
  Else
    Debug.Print "Download failed. Received HTTP status: " & HttpRequest.Status
  End If
Else
  Debug.Print "Expected Redirect. Received HTTP status: " & HttpRequest.Status
End If

Hard-coding "C:\foo.doc" does of course not make much sense. I'd use the file name the server supplies in the response headers ("Content-Type" or "Content-Disposition", depending on what you expect).

Tomalak
A: 

Another option besides the URLDownloadToFile API call suggested by Glomek is to use the AsyncRead method built into VB6.

MarkJ