zoukankan      html  css  js  c++  java
  • ASP.NET 和 WinForm 弹出另存为对话框

    1.ASP.NET

     Response.AddHeader("", "text/plain");
            Response.AddHeader("Content-Disposition", "attachment;filename=qwe" + DateTime.Now.ToString("yyyymmss")+".txt");
            Response.Write(str);
            Response.End(); 

    ASP.NET使用Page类里面的Response属性的AddHeader方法来制造另存为对话框,原理是给Http流添加键值对来进行文件流传输到浏览器的控制。

    键值对1:"","text/plain" 表示text/plain是无格式正文(同类的还有:text/html是html格式的正文,text/xml忽略xml头所指定编码格式而默认采用us-ascii编码,application/xml会根据xml头指定的编码格式来编码)

    键值对2:"Content-Disposition", "attachment;filename=xxx"  Content-Disposition用来激活IE读取文件时的文件下载对话框 attachment声明包含附件
    2.WINFORM

    SaveFileDialog svl = new SaveFileDialog();
            svl.FileName = "qwe" + DateTime.Now.ToString("yyyymmss") + ".txt";
            svl.Filter = "(*.txt)|*.txt|" + "(*.*)|*.*";
            svl.RestoreDirectory = true;
            if (svl.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(svl.FileName, true);
                sw.Write(str);
                sw.Close();
            }
            svl.Dispose();
    

     而WinForm则采用 SaveFileDialog控件来完成另存为对话框的弹出。

  • 相关阅读:
    131. Palindrome Partitioning
    130. Surrounded Regions
    129. Sum Root to Leaf Numbers
    128. Longest Consecutive Sequence
    125. Valid Palindrome
    124. Binary Tree Maximum Path Sum
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    120. Triangle
    119. Pascal's Triangle II
  • 原文地址:https://www.cnblogs.com/JhoneLee/p/2988740.html
Copyright © 2011-2022 走看看