Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

I have an url :-http://resources.printofast.com/media/bdccb931-1599-4eba-817d-6b3968ab90ee.jpg

I want to download this image using asp.net as it is .

Please let me know as i am unable to do it .

Many thanks

What I have tried:

1. string url = @"http://resources.printofast.com/media/bdccb931-1599-4eba-817d-6b3968ab90ee.jpg";
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png");
Posted
Updated 1-Nov-19 20:50pm

Here what i came up with..

public  string getImgFrmUrl(string url,string PartialDstntnPath ="/Content/img/Layout/")
       {

           var tmpP = PartialDstntnPath;
           var imgPath = System.Web.HttpContext.Current.Server.MapPath(tmpP);
           var name = Guid.NewGuid();
           var fileExt = Path.GetExtension(url);
           using (WebClient webClient = new WebClient())
           {
               const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
               const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
               ServicePointManager.SecurityProtocol = Tls12;
               try
               {
                   byte[] data = webClient.DownloadData(url);
                   using (MemoryStream mem = new MemoryStream(data))
                   {

                       using (var yourImage = Image.FromStream(mem))
                       {
                           if (fileExt.ToLower() == ".png")
                           {
                               yourImage.Save(imgPath + name + fileExt, ImageFormat.Png);
                           }
                           else
                           {
                               //yourImage.Save(Server.MapPath("/Upload/test.png"), System.Drawing.Imaging.ImageFormat.Png);
                               yourImage.Save(imgPath + name + fileExt, ImageFormat.Jpeg);
                           }
                       }
                   }
               }
               catch (Exception ex){
                   return ex.Message;
               }



           }
           return tmpP + name + fileExt;
       }
 
Share this answer
 
try this

C#
string url = "http://resources.printofast.com/media/bdccb931-1599-4eba-817d-6b3968ab90ee.jpg";
           WebClient webClient = new WebClient();
           string path = Server.MapPath("Images") ; // Create a folder named 'Images' in your root directory
           string fileName = Path.GetFileName(url);
           webClient.DownloadFile(url, path + "\\" + fileName);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900