how to download a file from remote server using asp.net

14,137

It would be easier to do it like this:

WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);
Share:
14,137
ush
Author by

ush

Updated on June 04, 2022

Comments

  • ush
    ush almost 2 years

    The below code works fine for downloading a file from a current pc.plz suggest me how to download it from remote server using ip address or any method

    protected void Button1_Click(object sender, EventArgs e)
    {
        const string fName = @"C:\ITFSPDFbills\February\AA.pdf";
        FileInfo fi = new FileInfo(fName);
        long sz = fi.Length;
    
        Response.ClearContent();
        Response.ContentType = MimeType(Path.GetExtension(fName));
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(fName)));
        Response.AddHeader("Content-Length", sz.ToString("F0"));
        Response.TransmitFile(fName);
        Response.End();
    }
    
    public static string MimeType(string Extension)
    {
        string mime = "application/octetstream";
        if (string.IsNullOrEmpty(Extension))
            return mime;
    
        string ext = Extension.ToLower();
        Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
        if (rk != null && rk.GetValue("Content Type") != null)
            mime = rk.GetValue("Content Type").ToString();
        return mime;
    }