Sign in to follow this  

downloading a file with c#

This topic is 3672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this