.net中 登录 才能下载文件的方法 Response.WriteFile实现下载

protected void Button2_Click(object sender, EventArgs e)  

        {  
        //可以在这里加是否登录的判断
string fileName = "chracater14.jpg";//客户端保存的文件名 (其他文件格式都支持) string filePath = Server.MapPath("../../images/chracater14.jpg");//路径 FileInfo fileInfo = new FileInfo(filePath); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(fileInfo.FullName); Response.Flush(); Response.End(); }

 

你可能感兴趣的:(response)