当前位置:编程学习 > C#/ASP.NET >>

文件下载

指定的文件路径  让客户下载  代码是怎么样的 
   求各位帮忙!!! --------------------编程问答--------------------

/// <summary> 
/// 文件下载 
/// </summary> 
/// <param name="savename">文件名</param> 
/// <param name="FullFileName">文件全名</param> 
/// <param name="Response">Response</param> 
public static void savefile(string savename,string FullFileName,System.Web.HttpResponse Response) 

try 

FileInfo DownloadFile = new FileInfo(FullFileName); 
if (DownloadFile.Exists) 

Response.Clear(); 
Response.ClearHeaders(); 
Response.Buffer = false; 
Response.ContentType = "application/octet-stream"; 
Response.AppendHeader("Content-Disposition", "attachment;filename="+ System.Web.HttpUtility.UrlEncode(savename,System.Text.Encoding.UTF8)); 
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); 
Response.WriteFile(DownloadFile.FullName); 
Response.Flush(); 
Response.End(); 

else 

//文件不存在 


catch 

//文件不存在 

--------------------编程问答--------------------
 protected void Page_Load(object sender, EventArgs e)
    {
        DownLoadFile(Server.MapPath("~/新文件夹1/http_imgload.jpg"));
    }
    public void DownLoadFile(string fname)
    {
        if (fname == "" || fname == string.Empty)
            return;
        FileInfo file = new FileInfo(fname);
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode((file.Name), System.Text.Encoding.UTF8));
        Response.AddHeader("content-length", file.Length.ToString());
        Response.ContentType = "image/jpeg";
        Response.WriteFile(file.FullName);
        Response.Flush();
        Response.Close();
        Response.End();
    }
--------------------编程问答-------------------- string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End(); --------------------编程问答-------------------- Response.ContentType = "application/x-zip-compressed";
        Response.AddHeader("Content-Disposition", "attachment;filename=kj.rar");
        string filename = Server.MapPath("DownLoad/kj.rar");
        Response.TransmitFile(filename);
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,