C# a标签请求下载文件
2024-08-22 20:33:57
服务器文件后台处理方式:
a标签:
<a href="/FileUpload/DownloadFile?file=/UploadFiles/File/bfcd676b-13a8-4195-b85b-59d9b9ca35e3.doc" title="下载"><span class="fa fa-arrow-circle-down"></span></a>
后台:
/// <summary>
/// 根据路径下载文件,主要用于生成的文件的下载
/// </summary>
/// <param name="file">文件路径</param>
/// <returns></returns>
public ActionResult DownloadFile(string file)
{
string realPath = Server.MapPath(file);
if (!FileUtil.IsExistFile(realPath))
{
return null;
}
string saveFileName = FileUtil.GetFileName(realPath); Response.WriteFile(realPath);
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel/msword";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(saveFileName));
Response.Flush();
Response.End(); return new FileStreamResult(Response.OutputStream, "application/ms-excel/msword");
}
二进制转文件处理方式(需要文件名和二进制数据):
a标签:
<a href="/DWGVersion/GetFilesByID?id=162" title="下载"><span class="glyphicon glyphicon-download"></span></a>
后台:
public ActionResult GetFilesByID(string id)
{
var dwgVersion = BLLFactory<DWGVersion>.Instance.GetDataById(id);
if (dwgVersion==null||dwgVersion.DrawingContent == null || dwgVersion.DrawingContent.Length == )
{
return null;
}
Response.Clear();
String strFileName = dwgVersion.FileName;//文件名称
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName));
Response.Buffer = true;
Response.BinaryWrite(dwgVersion.DrawingContent);//二进制数据
Response.Flush();
Response.End(); return new FileStreamResult(Response.OutputStream, "application/ms-excel/msword");
最新文章
- Windows“神器”收集贴
- HTML: xhtml和html的區別
- linux下mongodb定时备份指定的集合
- ios英语口语800句应用源码
- JS判断是不是本页面并且,给标签添加属性和属性值
- 如何选择Javascript模板引擎(javascript template engine)?
- 对同一元素设置overflow-x:hidden,overflow-y:visible;属性值不生效
- HTTP的长短连接、长短轮询的区别(转载)
- 将TIBCO Host 实例注册为Windows服务
- 入Lucene的第一个坑
- Mac下配置Nginx负载均衡
- app个推(透传消息)
- 方便快捷的求导求积分解方程在线工具sage介绍
- pinpoint初始化hbase脚本报错
- HDU 1115(求质量均匀分布的多边形重心 物理)
- pip install时遇到MemoryError的原因和处理方法
- 循环内的switch中break和continue使用区别
- 完整的AJAX
- C++并发编程实战---阅读笔记
- 富文本存储型XSS的模糊测试之道