Pergunta

I am trying to download and store an image file in VS2008, .NET Framework 3.5, but the resultant file is too small.

When I navigate to http://www-nass.nhtsa.dot.gov/nass/cds/GetBinary.aspx?Image&ImageID=682367204 in my browser, I am able to download it as a 68k file. However, using the following code, I get a file just under 4k:

using (WebClient webClient = new WebClient()) {
  webClient.DownloadFile("http://www-nass.nhtsa.dot.gov/nass/cds/GetBinary.aspx?Image&ImageID=682367204", @"D:\\682367204.jpg");
}

UPDATE: WebClient with credentials still not downloading file likely describes the source of (and solution to) my problem.

Foi útil?

Solução

Well I guess this is due to authorization restriction.

The reason why your code, Carlos and myself get a 4K file is because we have not the rights to access the requested file.

I guess you've used a sort of login procedure to access the site with your browser, or are on a trusted network allowing the site to use your NT tokens.

What seems to confirm this hypothesis is that when I change the extension of the GetBinary.aspx file to jpeg I obtain a kind of "forbidden" icon.

So you may have to do more work from your code to authenticate.

EDIT: using the 'NIX file utility with Cygwin gives this :

JPEG image data, JFIF standard 1.01

So what you get is indeed a JPEG image.

Outras dicas

It seems there is something wrong with that url. It is working for me in internet explorer but using google chrome the browser starts downloading "GetBinary.aspx" file, maybe that is the 4ks file you are getting.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top