效果:
思路:
简单的有两种方式下载,一种是流下载,一种是Writefile下载。以下是使用Writefile下载。
代码:
复制代码 代码如下:
protected voID linkbutton1_Click(object sender,EventArgs e)
{
try
{
//Writefile实现下载(word)
string filename = "qingPingguo.docx";//客户端保存的文件名
string filePath = Server.MapPath("~\\excel\\" + tb1.Text);//路径
fileInfo fileInfo = new fileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.Clearheaders();
response.addheader("Content-disposition","attachment;filename=" + filename);
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();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
/*************以下为流方式下载****************/
//string filename = "aaa.txt";//客户端保存的文件名
//string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
////以字符流的形式下载文件
//fileStream fs = new fileStream(filePath,fileMode.Open);
//byte[] bytes = new byte[(int)fs.Length];
//fs.Read(bytes,bytes.Length);
//fs.Close();
//Response.ContentType = "application/octet-stream";
////通知浏览器下载文件而不是打开
//response.addheader("Content-disposition","attachment; filename=" + httpUtility.UrlEncode(filename,System.Text.EnCoding.UTF8));
//Response.BinaryWrite(bytes);
//Response.Flush();
//Response.End();
}
以上是内存溢出为你收集整理的C#实现word文件下载的代码全部内容,希望文章能够帮你解决C#实现word文件下载的代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)