downloading a file with c#
Hi,
I'm trying to use the WebClient.DownloadClient method to get a file from the web. However, my url for the file is of the type: http://somewebsite.com/download.htm?ID=20&username=myusername&pass=mypass .
So, the url for the file is only given to me, after download.htm verifies all the arguments (ID, username and pass). After it does I get a zip file.
My question is, how can I download this file in c#?
Thank you
Googling about the subject gave me this result: Download And Upload A File Using C#.
I hope it helps you!
Cheers
I hope it helps you!
Cheers
Thanks, that problem is solved!
However, I have another problem: I want to save the file in my computer with the name it has on the server... How can I do it?
Thanks again
However, I have another problem: I want to save the file in my computer with the name it has on the server... How can I do it?
Thanks again
The only way I could do it so far (a very poor way), is by checking the ContentType value of my HttpResponse... this is bad however, because, imagine I'm trying to download a zip file, the content type can vary according to the compression used...
Any more ideas?
Thanks again
Any more ideas?
Thanks again
Depending on the implementation on the server you may receive a header called Content-Disposition which has two parts and should look like this:
"attachment; filename=somefilename"
The web client should contain this in its headers (see the Headers property or the headers property of your HttpResponse), but you will probably have to parse out the filename bit yourself.
If the response does not contain this header (it is an optional extension header I believe) you may have to fall back to parsing the file name from the original URL or simply generating an arbitrary name based on the Content-type.
Hope this helps.
"attachment; filename=somefilename"
The web client should contain this in its headers (see the Headers property or the headers property of your HttpResponse), but you will probably have to parse out the filename bit yourself.
If the response does not contain this header (it is an optional extension header I believe) you may have to fall back to parsing the file name from the original URL or simply generating an arbitrary name based on the Content-type.
Hope this helps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement