下载的文件打不开
这段代码想做的事是这样的,先在数据库表里找到某一条记录,然后找到该记录的一个字段:File_Name,最后根据指定的路径将该文件下载下来,可以下载,但打不开,下载后的文件变大了,请各位帮帮忙,问题出在哪?
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
con.Open();
string RpID = e.CommandArgument.ToString();
string cmdtext="select * from Customer_Item_Report where Report_No='"+RpID+"'";
SqlCommand cmd = new SqlCommand(cmdtext, con);
SqlDataReader dr = cmd.ExecuteReader();
try
{
if (dr.Read())
{
string fname = dr["File_Name"].ToString();
string filepath = Server.MapPath("UploadFile\\" + fname);
FileInfo files = new FileInfo(filepath);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fname, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-length", files.Length.ToString());
Response.WriteFile(filepath);
Response.Flush();
}
}
catch
{
Response.Write("<script>alert('没有找到下载的源文件')</script>");
}