zoukankan      html  css  js  c++  java
  • 多种下载文件方式 Response.BinaryWrite(byte[] DocContent);Response.WriteFile(System.IO.FileInfo DownloadFile .FullName);Response.Write(string html2Excel);

        通过html给xls赋值,并下载xls文件

    一、this.Response.Write(sw.ToString());System.IO.StringWriter sw = new System.IO.StringWriter();

               this.Response.Clear();
                string strFileName;
                strFileName = "报表" + ".xls";
                Response.Buffer = true;
                Response.ContentType = "application/x-zip-compressed";
                Response.AddHeader("Accept-Language", "zh-cn");
                Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(strFileName));

                System.IO.StringWriter sw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(sw);

                this.gvMutiQueResult.AllowPaging = false;
                if (Session["QueryResult"] != null)
                {
                    this.gvMutiQueResult.DataSource = (DataTable)Session["QueryResult"];
                    this.gvMutiQueResult.DataBind();
                }
                else
                {
                    BindGV(); 绑定数据
                }

                this.gvMutiQueResult.RenderControl(htmlWrite);
                this.Response.Write(sw.ToString());
                this.Response.End();
                Response.Write("<script type='text/javascript'>window.history.back();</script>");

    二、Response.Write(html2Excel);string html2Excel

    string strHtml; string fileName;

     Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename="" + HttpUtility.UrlEncode(fileName) + ".xls"");
                Response.ContentType = "application/ms-excel";

                string html2Excel = "<html xmlns:x="urn:schemas-microsoft-com:office:excel"><head><meta content="text/html;charset=UTF-8" http-epuiv="content-type">"
                    +
                     string.Format(@"<!--[if gte mso 9]><xml>
                <x:ExcelWorkbook>
                    <x:ExcelWorksheets>
                        <x:ExcelWorksheet>
                            <x:Name>{0}</x:Name>
                            <x:WorksheetOptions>
                                <x:Print>
                                    <x:ValidPrinterInfo />
                                </x:Print>
                            </x:WorksheetOptions>
                        </x:ExcelWorksheet>
                    </x:ExcelWorksheets>
                </x:ExcelWorkbook>
            </xml>
            <![endif]-->", fileName)
                     + "</head><body>"
                    + strHtml +
                    "</body></html>";

                Response.Write(html2Excel);
                Response.End();

    三、

    //获取数据
                    IDataBase sDB = DBFactory.GetDBInstance();
                    DataTable dt = sDB.GetDataTable("select * from 表 where ID='" + ID.ToString() + "'");
                    //输出
                    if (dt.Rows.Count > 0)
                    {

                        string DocFileName = dt.Rows[0]["DocFileName"].ToString();
                        string DocSize = dt.Rows[0]["DocSize"].ToString();
                        byte[] DocContent = (byte[])dt.Rows[0]["DocContent"];

                        Response.Clear();
                        Response.ClearHeaders();
                        Response.Buffer = false;
                        //string ContentType = dt.Rows[0]["DocType"].ToString();
                        Response.ContentType = "application/octet-stream";
                        Response.AddHeader("Content-Disposition", "attachment; filename="" + HttpUtility.UrlEncode(DocFileName, System.Text.Encoding.UTF8) + """);
                        Response.AddHeader("Content-Length", DocSize.ToString());
                        Response.BinaryWrite(DocContent);
                        Response.Flush();
                        Response.End();

                    }

    四、Response.WriteFile(DownloadFile.FullName); System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFileName);

     //下载开机提醒安装程序 rar
            private void DownloadDocument()
            {
                try
                {
                    string strFileName = Server.MapPath("../../help/BootAlert.rar");
                    System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFileName);
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.Buffer = false;
                    Response.ContentType = "application/ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
                    Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                    Response.WriteFile(DownloadFile.FullName);
                    Response.Flush();
                    Response.End();
                }
                catch (Exception err)
                {
                    throw err;
                }
            }

    五、 byte[] binaryContent  Response.BinaryWrite(binaryContent);

    newFileName = newFileName.Replace("\\", "\").Replace("\", "/");
                    string outputFileName = newFileName.Substring(newFileName.LastIndexOf('/') + 1);
                    byte[] binaryContent = System.IO.File.ReadAllBytes(newFileName);
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.Buffer = false;
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename="" + HttpUtility.UrlEncode(outputFileName, System.Text.Encoding.UTF8) + """);
                    Response.AddHeader("Content-Length", binaryContent.Count().ToString());
                    Response.BinaryWrite(binaryContent);
                    Response.Flush();
                    Response.End();

    六、Response.TransmitFile(strFilePath, 0, fileSize);long fileSize = info.Length

                        FileInfo info = new FileInfo(strFilePath);
                        long fileSize = info.Length;
                        Response.Clear();
                        Response.Buffer = true;
                        Response.ContentType = "application/x-zip-compressed";
                        Response.AddHeader("Accept-Language", "zh-cn");
                        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(info.Name));
                        //不指明Content-Length用Flush的话不会显示下载进度   
                        Response.AddHeader("Content-Length", fileSize.ToString());
                        Response.TransmitFile(strFilePath, 0, fileSize);
                        Response.Flush();
                        Response.Close();
                        info.Delete();

  • 相关阅读:
    Self Numbers
    【acdream】小晴天老师系列——竖式乘法
    全错位排列
    2 ^ x mod n = 1问题
    基于cocos2dx的横版动作游戏制作(二)
    基于cocos2dx的横版动作游戏制作(一)
    横版游戏制作之英雄技能CD遮罩,人物头像血条属性
    cocos2d横版游戏之摇杆控制
    C++ delete []p 数组指针,如何知道该数组大小的
    do { ....} while(0) 在宏里冗余的意义
  • 原文地址:https://www.cnblogs.com/xuxin-1989/p/3897355.html
Copyright © 2011-2022 走看看