我想实现一个在线下载或者打开磁盘上文件的功能,就像保存word一样? 请哪位高人帮我看看这段代码有没有问题啊?为什么运行成功了什么都弹不出来?
主界面代码:
HlkURL.NavigateUrl ="webForm1.aspx?Name=" +MyDataRowView.Row ["Name"].ToString();WEBFORM1界面上代码:
private void Page_Load(object sender, System.EventArgs e)
{
opendb();
}
private void opendb()
{
string filename=Request.QueryString["Name"].ToString();
string sql="select * from T_PeopleInfo where Name='"+filename+"'";
connstr();
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
filename=Server.MapPath(".")+"\\"+"work/"+filename;//文件存放在word文件夹里面
if(!System.IO.File.Exists(filename))
{
Response.Write("<script>alert('文档不存在!');</script>");
return;
}
System.IO.FileInfo file = new System.IO.FileInfo(filename);
Response.ContentType = "application/octet-stream";
Response.Clear();
Response.Charset = "GB32";
Response.AddHeader("Content-Disposition", "attachment;filename="+System.Web.HttpUtility.UrlEncode(file.Name,System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.Clear();
Response.End();
}
dr.Close();
conn.Close ();
}
主界面代码:
HlkURL.NavigateUrl ="webForm1.aspx?Name=" +MyDataRowView.Row ["Name"].ToString();WEBFORM1界面上代码:
private void Page_Load(object sender, System.EventArgs e)
{
opendb();
}
private void opendb()
{
string filename=Request.QueryString["Name"].ToString();
string sql="select * from T_PeopleInfo where Name='"+filename+"'";
connstr();
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
filename=Server.MapPath(".")+"\\"+"work/"+filename;//文件存放在word文件夹里面
if(!System.IO.File.Exists(filename))
{
Response.Write("<script>alert('文档不存在!');</script>");
return;
}
System.IO.FileInfo file = new System.IO.FileInfo(filename);
Response.ContentType = "application/octet-stream";
Response.Clear();
Response.Charset = "GB32";
Response.AddHeader("Content-Disposition", "attachment;filename="+System.Web.HttpUtility.UrlEncode(file.Name,System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.Clear();
Response.End();
}
dr.Close();
conn.Close ();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货