How to download an image using a URL with a query string instead of image path?

主宰稳场 提交于 2020-01-03 05:26:21

问题


I'm doing a C# windows application, and need to know how to download a picture which has a link with a query string, for example www.mywebsite.com/img.aspx?imgid=12345 (which automatically redirects to the appropriate image based on imgid). I need to then save the file to the disk somewhere. I don't need any big fancy download manager, just the cleanest way to get such an image from a redirecting URL.


回答1:


You should be able to do it like this:

using (WebClient Client = new WebClient ())
{
    Client.DownloadFile("http://www.mywebsite.com/img.aspx?imgid=12345", "12345.jpg");
}



回答2:


Use the WebClient class and he DownloadFile method. http://msdn.microsoft.com/en-us/library/ez801hhe.aspx

It's perfectly acceptable to pass a querystring parameter in the URI of the method.




回答3:


Your file will be at bin/debug folder, and give the extension in which your browser download the file. like

{
            using (WebClient client = new WebClient()) {
                client.DownloadFile("http://www.mywebsite.com/img.aspx?imgid=12345", "selectedfile.gif");
            }
        }


来源:https://stackoverflow.com/questions/8197294/how-to-download-an-image-using-a-url-with-a-query-string-instead-of-image-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!