Copy image file from web url to local folder?

后端 未结 4 1263
刺人心
刺人心 2021-02-02 18:01

I have a web URL for the image. For example \"http://testsite.com/web/abc.jpg\". I want copy that URL in my local folder in \"c:\\images\\\"; and also when I copy that file into

相关标签:
4条回答
  • 2021-02-02 18:43
    string path = "~/image/"; 
    string picture = "Your picture name with extention";
    path = Path.Combine(Server.MapPath(path), picture);
    using (WebClient wc = new WebClient())
                                {
    wc.DownloadFile("http://testsite.com/web/abc.jpg", path);
                                }
    

    Its works for me

    0 讨论(0)
  • 2021-02-02 18:46

    thats not too difficult. Open a WebCLient and grab the bits, save them locally....

    using ( WebClient webClient = new WebClient() ) 
    {
       using (Stream stream = webClient.OpenRead(imgeUri))
       {
          using (Bitmap bitmap = new Bitmap(stream))
          {
             stream.Flush();
             stream.Close();
             bitmap.Save(saveto);
          }
       }
    }
    
    0 讨论(0)
  • 2021-02-02 18:58

    Request the image, and save it. For example:

    byte[] data;
    using (WebClient client = new WebClient()) {
      data = client.DownloadData("http://testsite.com/web/abc.jpg");
    }
    File.WriteAllBytes(@"c:\images\xyz.jpg", data);
    
    0 讨论(0)
  • 2021-02-02 18:59

    You could use a WebClient:

    using (WebClient wc = new WebClient())
        wc.DownloadFile("http://testsite.com/web/abc.jpg", @"c:\images\xyz.jpg");
    

    This assumes you actually have write rights to the C:\images folder.

    0 讨论(0)
提交回复
热议问题