6

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.

2
  • 3
    A page is another type of file. What exactly is your criteria? Do you want to ignore all text files, or just any page that includes HTML, or something different? What about a .css file, for example? Or a .jpg that is part of a page? Ultimately I suspect you're looking for something MIMEType-related, but you're not really giving a clear requirement.
    – Dan Puzey
    Sep 16, 2013 at 13:17
  • Sorry i wasn't all that clear, all i really needed was to find out if its a html page or another type of file and then proceed to download the file if it is not an html file.
    – Nick V
    Sep 16, 2013 at 13:28

1 Answer 1

10

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.

2
  • 3
    or, if it suits your purpose, only make a HEAD request to find out the MIME as described in this post: stackoverflow.com/questions/14030245/…
    – olydis
    Sep 16, 2013 at 13:19
  • 1
    Thanks this is what i needed! i'll accept your answer when i can.
    – Nick V
    Sep 16, 2013 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.