Download image from url and save in folder (directory) using C# and VB.net in ASP.Net

nauna
 
on Oct 06, 2021 01:55 PM
Sample_256921.zip
773 Views

hello,

How to download image from url and store in folder in asp.net

https://blackwebsite.s3.us-east-2.amazonaws.com/42431c75-9311-4730-960a-4052f2427ab9.png

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 07, 2021 04:28 AM

Hi nauna,

Refer below sample.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string url = "https://blackwebsite.s3.us-east-2.amazonaws.com/42431c75-9311-4730-960a-4052f2427ab9.png";
    if (!System.IO.Directory.Exists(Server.MapPath("~/Files")))
    {
        System.IO.Directory.CreateDirectory(Server.MapPath("~/Files"));
    }
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        client.DownloadFile(new Uri(url), Server.MapPath("~/Files/42431c75-9311-4730-960a-4052f2427ab9.png"));
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim url As String = "https://blackwebsite.s3.us-east-2.amazonaws.com/42431c75-9311-4730-960a-4052f2427ab9.png"
    If Not IO.Directory.Exists(Server.MapPath("~/Files")) Then
        IO.Directory.CreateDirectory(Server.MapPath("~/Files"))
    End If
    Using client As Net.WebClient = New Net.WebClient()
        client.DownloadFile(New Uri(url), Server.MapPath("~/Files/42431c75-9311-4730-960a-4052f2427ab9.png"))
    End Using
End Sub