zoukankan      html  css  js  c++  java
  • C# a标签请求下载文件

    服务器文件后台处理方式:

    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 == 1)
                    {
                        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");
  • 相关阅读:
    9-python 的ProxyHandler处理器(代理设置)
    2018.2.7 css 的一些方法盒子模型
    2018.2.6 JS-判断用户浏览器
    2018.2.5 PHP如何写好一个程序用框架
    2018. 2.4 Java中集合嵌套集合的练习
    2018.2.3 Centos 的vim好看的主题配置及JDK的安装配置
    2018.2.2 java中的Date如何获取 年月日时分秒
    2018.2.2 JavaScript中的封装
    2018.1.30 PHP编程之验证码
    2018.1.29 计算机二级错题汇总(二)
  • 原文地址:https://www.cnblogs.com/xiaojitui/p/9167172.html
Copyright © 2011-2022 走看看