Download file from URL using C# and VB.Net in ASP.Net

Last Reply on Apr 19, 2018 12:28 AM By kalpesh

Posted on Apr 18, 2018 11:58 PM

i need to download 1 file from my database i use this 

            database bl = new database();
            var url = JsonConvert.SerializeObject(bl.getViewDocument(in_master_do_id, in_do_id, in_doc_type, in_view_type));

            Stream stream = null;
            int bytesToRead = 10000;
            var fileName = "test";
            byte[] buffer = new Byte[bytesToRead];
            try
            {
                HttpWebRequest urlRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                HttpWebResponse urlResponse = (HttpWebResponse)urlRequest.GetResponse();
                if (urlRequest.ContentLength > 0)
                    urlResponse.ContentLength = urlRequest.ContentLength;
                stream = urlResponse.GetResponseStream();
                var resp = HttpContext.Current.Response;
                resp.ContentType = "application/octet-stream";
                resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
                resp.AddHeader("Content-Length", urlResponse.ContentLength.ToString());
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            //return JsonConvert.SerializeObject(bl.getViewDocument(in_master_do_id, in_do_id, in_doc_type, in_view_type));
            return url;

but i dont know what is missign this doesnt work  plz helo 

You are viewing reply posted by: kalpesh on Apr 19, 2018 12:28 AM.
Posted on Apr 19, 2018 12:28 AM Modified on on Apr 19, 2018 12:41 AM

You can refer below link for your reference also you can refer below sample code for your reference.

How to convert any URL to Image in ASP.Net

Refer the below sample code for your reference.

C#

protected void Page_Load(object sender, EventArgs e)
{
    System.Net.WebClient webClient = new System.Net.WebClient();
    string url = "https://static.flickr.com/66/199481236_dc98b5abb3_s.jpg";        
    byte[] bytes = webClient.DownloadData(url);
    string fileName = (url.Split('/')[url.Split('/').Length - 1]).Split('.')[0];
    Response.ContentType = "image/png";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.End();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim webClient As System.Net.WebClient = New System.Net.WebClient()
    Dim url As String = "https://static.flickr.com/66/199481236_dc98b5abb3_s.jpg"
    Dim bytes As Byte() = webClient.DownloadData(url)
    Dim fileName As String = (url.Split("/"c)(url.Split("/"c).Length - 1)).Split("."c)(0)
    Response.ContentType = "image/png"
    Response.AppendHeader("Content-Disposition", "attachment; filename=" & fileName)
    Response.BinaryWrite(bytes)
    Response.[End]()
End Sub

Screenshot