You must Sign In to post a response.
  • Category: Sharepoint

    How to download sharepoint files programatically

    Hi,

    In my application i wanted to download the sharepoint file programatically in a webpart.
    for that i have used below code.It is getting downloaded but no save dialog box is coming , page is refreshing and file is getting downloaded, no intimations.
    /////////
    HttpWebRequest request;
    HttpWebResponse response = null;
    string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    string pathDownload = Path.Combine(pathUser, "Downloads");
    request = (HttpWebRequest)WebRequest.Create(strURL);
    request.Credentials = System.Net.CredentialCache.DefaultCredentials;
    request.Timeout = 20000;
    request.AllowWriteStreamBuffering = true;
    response = (HttpWebResponse)request.GetResponse();
    Stream s = response.GetResponseStream();

    if (!Directory.Exists("C:\\Users\\Administrator\\Downloads"))
    {
    Directory.CreateDirectory(@"C:\Users\Administrator\Downloads");
    }

    string aFilePath = "C:\\Users\\Administrator\\Downloads" + "\\" + strFileName;
    FileStream fs = new FileStream(aFilePath, FileMode.Create);
    byte[] read = new byte[256];
    int count = s.Read(read, 0, read.Length);
    while (count > 0)
    {
    fs.Write(read, 0, count);
    count = s.Read(read, 0, read.Length);
    }
    fs.Close();
    s.Close();
    response.Close();
    ///////

    So how can we put save dialog box here?

    Thanks
    Ginnas
  • #756744
    Hi,

    try the below method.

    rivate void DownloadFile( string fname, bool forceDownload )
    {
    string path = MapPath( fname );
    string name = Path.GetFileName( path );
    string ext = Path.GetExtension( path );
    string type = "";
    // set known types based on file extension
    if ( ext != null )
    {
    switch( ext.ToLower() )
    {
    case ".htm":
    case ".html":
    type = "text/HTML";
    break;

    case ".txt":
    type = "text/plain";
    break;

    case ".doc":
    case ".rtf":
    type = "Application/msword";
    break;
    }
    }
    if ( forceDownload )
    {
    Response.AppendHeader( "content-disposition",
    "attachment; filename=" + name );
    }
    if ( type != "" )
    Response.ContentType = type;
    Response.WriteFile( path );
    Response.End();
    }

    Thanks and Regards,
    M.Anandha Babu


  • Sign In to post your comments