views:

84

answers:

2

Okay so I want to download a file from a website, but the file is lacking an extension. (it's an image file, I know this much, but the link does not provide the actual extension)

When I use webrequest, or webclient to download the file I get a "404 file not found" exception.

        WebClient wc = new WebClient();

        Stream strm = wc.DownloadFile("http://some_site.some_domain/some_image.","C:/some_directory/save_name.some_extention");

Notice the lack of extention at the end of the URL. The site in question displays the image fine in a webbrowser, but when viewing just the image there is no extension and thus it's treated an unknown file (not showing an image).

So simply put: how do I download a file if there is no extention specified?

Thanks in advance!

A: 

So you're trying to determine what extension to give the file after downloading? If the URL doesn't have one you would have to inspect the actual data of the file.

You might be able to inspect the beginning of the file and see if it matches known valid file types. For instance, PNGs seem to have 'PNG' as bytes 2-4 (at least in the ones I've inspected). By looking at that data you should be able to determine the format with a fairly high accuracy.

Herms
Even though this might prove helpfull later on, this is not the thing I'm struggling with. I'm not even getting to the part where I can inspect a steam or save it as anything for that matter, as it doesn't retrieve the data, it just throws a "404 not found" exception. Even though if I were to copy-paste the link of the file in a web browser it would prompt me with a 'where would you like to save this file.' dialog. (As an unknown file, but that's not the main issue.)
Strike
This is the point where I'd pull out Wireshark (http://www.wireshark.org/). Do the request from the browser, then do it from your app. See what's different. Perhaps your request is getting mangled somehow, or you're missing some header information that might be needed, or maybe your request is never leaving your machine. Wireshark should help narrow it down.
Herms
A: 

This would be my best suggestion, if this doesn't work I don't know how to solve you problem...

List<string> fileExtensions = new List<string>(){"png","gif","bmp","jpg"}// other known image file extensions here...

WebClient wc = new WebClient();
foreach(var extension in fileExtensions)
{
  try
  {     wc.DownloadFile("http://some_site.some_domain/some_image."+extension,"C:/some_directory/save_name."+extension);
   break;
  }
  catch {}
}

This would just be a work around, I guess... Not a real solution...

EJC
It's a work around, as in that it stops the programming crashing, other than that it still doesn't get the data from the server. Thanks for trying though! (it's just like having a file in windows, without an extension, so adding one would change the file reference entirely, or that's what I think anyway)
Strike
Ok, sorry I couldn't be of more help...
EJC