日期:2014-05-18  浏览次数:20364 次

关于在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 ".html":
                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