在asp.net C#中从服务器下载文件到客户端 [英] Download file from server to client in asp.net C#

查看:511
本文介绍了在asp.net C#中从服务器下载文件到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string path = (@"D:\Doc\Offer letter.pdf"); //get physical file path from server
    string name = Path.GetFileName(path); //get file name
    string ext = Path.GetExtension(path); //get file extension
    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 ".GIF":
                type = "image/GIF";
                break;

            case ".pdf":
                type = "Application/pdf";
                break;

            case ".doc":
            case ".rtf":
                type = "Application/msword";
                break;
        }
    }

    Response.AppendHeader("content-disposition", "attachment; filename=" + name);

    if (type != "")
        Response.ContentType = type;
    Response.WriteFile(path);

    Response.End(); //give POP to user for file downlaod







i有这个代码用于从服务器下载文件到客户端。但是这个代码还没有显示任何一个doanload和save的dailogue box.Please任何人帮助我。




i have this code for downloading file from server to client. But this code not yet all showing any dailogue box for doanload and save.Please any one help me.

推荐答案

你可以使用这个代码...



u can use this code...

 protected void lnkfilepath_Click(object sender, EventArgs e) // ur link button 
 {   
     string filename = lnkfilepath.Text;
     string Filpath = Server.MapPath("~/Attachments/" + filename);
     DownLoad(Filpath);     }
     public void DownLoad(string FName){ 
     string path = FName;  
     System.IO.FileInfo file = new System.IO.FileInfo(path);   
     if (file.Exists)  {   
     Response.Clear();  
     Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);                         Response.AddHeader("Content-Length", file.Length.ToString()); 
     Response.ContentType = "application/octet-stream"; // download […]
}


虽然帖子很旧,但提交任何有同样问题的新用户:



如果你的问题只是没有得到对话框,那么它取决于浏览器,如果它已被检查为不再要求选择,它就不会出现再次。你不用担心这个。



相同的代码可以通过带保存按钮的对话框下载另一个用户的文件。在不同的浏览器中查看即。 Chrome和Firefox。
Though the post is very old, submitting for any new user having the same kind of issue:

If your problem is just "not getting the dialog box", then it's browser dependent and if it's already checked as 'Do not ask again' kind of selection, it wont appear again. You need not worry about this.

Same code may download the files for another user with dialog box with save button. Check it in different browser ie. Chrome and Firefox.


这篇关于在asp.net C#中从服务器下载文件到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆