IE直接下载汇总

 JS效果:

--------------------------------------------------------------------------------
<a href="#" onclick="openfile()">下载</a>
<script language="JavaScript">
 function openfile()
  {
   var a =window.open("aaa.txt","_blank","");
   a.document.execCommand("SaveAs");
   a.close();
  }
</script>

--------------------------------------------------------------------------------
<script language="C#">
<!--
    /**//// <summary>
    /// 用于文件的下载
    /// </summary>
    /// <param name="id">要下载的文件编号</param>
    private void DownLoadDocument(string id)
     {
       //命名空间 缩写,
        ****.BLL.Document bll = new XTMYDocMgmt.BLL.Document();
        ****.Model.Document model = bll.GetModel(Convert.ToInt32(id));

        string rootPath  = @Server.MapPath("~") + model.DocPath;
        FileInfo toDownload = new FileInfo(rootPath);

        //文件扩展名
        string strExt = Path.GetExtension(rootPath);

        Response.Clear();
        //Response.ContentType = "application/x-zip-compressed";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        //文件保存时的名字。。处理中文乱码和名字中有空格的问题
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(model.DocName).Replace("+","%20") + strExt);

        //Response.WriteFile(rootPath);
        Response.TransmitFile(rootPath);

        Response.End();
    }

//-->
</script>

 --------------------------------------------------------------------------------

<script language="c#">
<!--
//用流的方法
Response.AddHeader("Content-Disposition",   "attachment;   filename=aaa.txt"   );  
Response.ContentType   =   "application/octet-stream";  
Response.Flush();  
Response.Close();
 
//-->
</script>


--------------------------------------------------------------------------------
<script language="c#">
<!--

//方法二

Response.AddHeader("Content-Disposition",   "attachment;   filename=aaa.txt"   );  
System.IO.StreamReader   sr   =   new   StreamReader(@"c:\aaa.txt",System.Text.Encoding.Default);  
Response.Write(sr.ReadToEnd());  
sr.Close();  
Response.Flush();  
Response.Close();

//-->
</script>


 
--------------------------------------------------------------------------------

其他还可以通过IIS设置注册一下
 


我在win2003+iis6中建了个web在显示txt文件时:直接出现下载的页面。。。而不能显示内容。。。  
  我已经在MIME中加入.txt   txt/plain   ,我现在想直接在web中显示文本内容

posted @ 2007-07-31 10:46  Winner.Net(2007)  阅读(505)  评论(0编辑  收藏  举报