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");
  • 相关阅读:
    Eclipse中自动提示的方法参数都是arg0,arg1的解决方法
    eclipse 下找不到或无法加载主类的解决办法
    将博客搬至CSDN
    java接口中定义成员变量
    重写与重载
    多位数每一位个系数:个位num%10;十位num/10%10.......
    输出 n=6 的三角数字阵(JAVA基础回顾)
    二维数组的遍历之查漏补缺
    For循环打印正三角,倒三角,菱形
    JAVA的continue用法
  • 原文地址:https://www.cnblogs.com/xiaojitui/p/9167172.html
Copyright © 2011-2022 走看看