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");
  • 相关阅读:
    股票F10
    什么是盘口?
    CompletionPort
    WSAEvent
    SO_KEEPALIVE选项
    SO_LINGER
    shutdown和close的区别
    TIME_WAIT和CLOSE_WAIT状态区别
    什么是2MSL
    WSAStartup function
  • 原文地址:https://www.cnblogs.com/xiaojitui/p/9167172.html
Copyright © 2011-2022 走看看