当前位置: 代码迷 >> ASP.NET >> 关于在repeater里的linkbutton实现下载功能解决方案
  详细解决方案

关于在repeater里的linkbutton实现下载功能解决方案

热度:7219   发布时间:2013-02-25 00:00:00.0
关于在repeater里的linkbutton实现下载功能
C# code
  protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)    {        if (e.CommandName.Equals("DownLoad"))        {            string[] strs = e.CommandArgument.ToString().Split(',');            //strs[0]为目录路径,strs[1]为文件名            string filePath = "System/UploadFiles/" + GetPath(strs[0])+strs[1];            FileInfo fileInfo = new FileInfo(filePath);            string fileExt = fileInfo.Extension.Trim().ToLower();            Response.Clear();            Response.ClearContent();            Response.ClearHeaders();            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strs[1]));            Response.AddHeader("Content-Length", fileInfo.Length.ToString());            Response.AddHeader("Content-Transfer-Encoding", "binary");            Response.ContentType = checktype(HttpUtility.UrlEncodeUnicode(fileExt));//"application/octet-stream";            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");            Response.WriteFile(fileInfo.FullName);            Response.Flush();            Response.End();            //Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);            //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();        }    }    public string checktype(string fileExt)    {        string ContentType;        switch (fileExt)        {            case ".asf":                ContentType = "video/x-ms-asf"; break;            case ".avi":                ContentType = "video/avi"; break;            case ".doc":                ContentType = "application/msword"; break;            case ".zip":                ContentType = "application/zip"; break;            case ".rar":                ContentType = "application/x-zip-compressed"; break;            case ".xls":                ContentType = "application/vnd.ms-excel"; break;            case ".gif":                ContentType = "image/gif"; break;            case ".jpg":                ContentType = "image/jpeg"; break;            case "jpeg":                ContentType = "image/jpeg"; break;            case ".wav":                ContentType = "audio/wav"; break;            case ".mp3":                ContentType = "audio/mpeg3"; break;            case ".mpg":                ContentType = "video/mpeg"; break;            case ".mepg":                ContentType = "video/mpeg"; break;            case ".rtf":                ContentType = "application/rtf"; break;            case "":                ContentType = "text/html"; break;            case ".htm":                ContentType = "text/html"; break;            case ".txt":                ContentType = "text/plain"; break;            default:                ContentType = "application/octet-stream";                break;        }        return ContentType;    }

却提示
C# code
未能找到文件“System/UploadFiles/Agent/txtFile.jpg”。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IO.FileNotFoundException: 未能找到文件“System/UploadFiles/Agent/txtFile.jpg”。源错误: 行 155:            Response.ClearHeaders();行 156:            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strs[1]));行 157:            Response.AddHeader("Content-Length", fileInfo.Length.ToString());行 158:            Response.AddHeader("Content-Transfer-Encoding", "binary");行 159:            Response.ContentType = checktype(HttpUtility.UrlEncodeUnicode(fileExt));//"application/octet-stream";
  相关解决方案