Reputation: 707
We need the following:
Download a file from an URL if it is actually a file. Otherwise if its a page do nothing.
For a quick example I've got the following to download a file:
My.Computer.Network.DownloadFile(
"http://www.wired.com/wiredenterprise/wp-content/uploads/2013/07/ff_googleinfrastructure_large.jpg",
"d:\ff_googleinfrastructure_large.jpg")
But if we got a normal web page for example "http://www.google.com" it will just download the page which is something we do not want.
So how can i find out if an URL will lead to a file instead of a page?
It could be any type of file so checking if the URL ends with .zip or .jpg or .docx or... simply won't do.
Answers in VB.NET or C# are both welcome which is why i marked both.
Upvotes: 6
Views: 3010
Reputation: 38825
Ahead of time, there's no 100% accurate way. You could check the extension (assuming there is one), but even that is not 100% foolproof.
You could make the request and examine the content-type
header and bail out of downloading the file if the value is text/html
or some text
MIME variant. As olydis points out below, you can perform a HEAD
request to just get the response header back and decide then if you want to download the file in its entirety at that point.
Upvotes: 10