asp.net下载文件

来源:互联网 发布:mysql 字符长度函数 编辑:程序博客网 时间:2024/05/23 01:13
try
        {
           string resourceurl=".//路径//文件名.扩展名";
            int i =  resourceurl.LastIndexOf('\\');
            string filename=resourceurl.Substring(i + 1);
            string FullFileName = Server.MapPath(resourceurl);
            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=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.ASCII));
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(),"alert","alert('文件不存在!')",true);
            }
        }
        catch
        {
            throw;
        } 
原创粉丝点击