Page view counter

Download

Last post 12-19-2007 10:50 PM by cafeasp. 5 replies.

Sort Posts:

  • Download

    12-17-2007, 5:54 AM

    Hi,

    Does anybody have code to download a file from the remote server using C#.net.

    I have a code that downloads a file with in the the local system. given below.

    string path = "File path comes here";

    System.IO.FileInfo file = new System.IO.FileInfo(path); //get file object as FileInfo

     

    if (file.Exists) //-- if the file exists on the server

    {

    Response.Clear(); //set appropriate headers

    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

    Response.AddHeader("Content-Length", file.Length.ToString());

    Response.ContentType = "application/....";

    Response.WriteFile(file.FullName);

    Response.End(); //if file does not exist

     but my requirement is to download a file from the remote server

    Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post fixed your problem.
  • Re: Download

    12-17-2007, 7:01 AM
    Answer

    here is the referenced link..

    Programmatically Download File from Remote Location to User Through Server


     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Data.Sql;
    using System.Net;
    using System.IO;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    //base.OnLoad(e);
    string url = string.Empty;// Request.QueryString["DownloadUrl"];
    if (url == null || url.Length == 0)
    {
    url = "http://img444.imageshack.us/img444/6228/initialgridsq7.jpg";
    }

    //Initialize the input stream
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    int bufferSize = 1;

    //Initialize the output stream
    Response.Clear();
    Response.AppendHeader("
    Content-Disposition:", "attachment; filename=download.jpg");
    Response.AppendHeader("
    Content-Length", resp.ContentLength.ToString());
    Response.ContentType = "
    application/download";

    //Populate the output stream
    byte[] ByteBuffer = new byte[bufferSize + 1];
    MemoryStream ms = new MemoryStream(ByteBuffer, true);
    Stream rs = req.GetResponse().GetResponseStream();
    byte[] bytes = new byte[bufferSize + 1];
    while (rs.Read(ByteBuffer, 0, ByteBuffer.Length) > 0)
    {
    Response.BinaryWrite(ms.ToArray());
    Response.Flush();
    }

    //Cleanup
    Response.End();
    ms.Close();
    ms.Dispose();
    rs.Dispose();
    ByteBuffer = null;
    }
    }
     working fine for me...

    hope it helps./. 

    Thanx,
    [KaushaL] || BloG || Microsoft MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: Download

    12-17-2007, 11:39 AM
    • Loading...
    • Haissam
    • Joined on 10-05-2006, 6:25 AM
    • Beirut - Lebanon
    • Posts 5,632
    • Points 37,391

    Check below link

    Download files in asp.net

    HC 

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Re: Download

    12-18-2007, 12:52 PM
    Answer

    Hi,

     

    I should say that i am very much thankful to u.

    This code helped me alot.

    ThankU kaushalparik.

     

    Regards,

    Vallamreddy VenuGopal

    Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post fixed your problem.
  • Re: Download

    12-19-2007, 8:41 PM
    • Loading...
    • SlackmasterK
    • Joined on 05-11-2006, 6:44 PM
    • Colorado
    • Posts 11
    • Points 39

    kaushalparik27:
    here is the referenced link..

    Programmatically Download File from Remote Location to User Through Server


    [...]

     working fine for me...

    hope it helps./. 

    W00t, my site got referenced! I saw an incoming click and referenced back here. Hope it helps.

    Slackmaster K
    Proprietor, DamnedNice.Com
    VB.NET / C# / ASP.NET
    Athalon XP Barton 4200+, 3.25GB, 4.2TB
    Intel Core 2 Quad Q6600, 2GB, 6 Screens
    Intel Core 2 Duo 2.33GHz, 2GB
  • Re: Download

    12-19-2007, 10:50 PM
    • Loading...
    • cafeasp
    • Joined on 04-12-2007, 8:18 PM
    • Posts 101
    • Points 545

    Very nice code Kausha

    Keep it simple !
Page 1 of 1 (6 items)