asp.net IE 中直接打开 Excel 文件,不要弹出对话框
每一次都弹出打开,还是保存的确认对话框很麻烦,
想要按按钮后就直接打开一个excel文件

解决方案 »

  1.   

      int id= Convert.ToInt32( Request.QueryString["docId"],10);
        string ConnStr=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
        string str_get_file="select * from fmfilecont where fileid="+id;
        SqlConnection Conn = new SqlConnection(ConnStr); 
        SqlCommand myCommand=new SqlCommand(str_get_file,Conn);
        Conn.Open();    SqlDataReader dr = myCommand.ExecuteReader();    if(dr.Read())
        {
         Response.ContentType = (string)dr["ftype"];
         Response.OutputStream.Write((byte[])dr["cont"], 0, (int)dr["Size"]);
        }
        else
        {
         Response.Write("没有这个文件");
         Response.End();
        }    dr.Close();
        myCommand.Connection.Close();
      

  2.   

    这个与客户端设置有关但可以设置Content-type等http头试试
      

  3.   

    我记得是这样设置
    IIS--》右键默认网站--》HTTP头--》文件类型--》新类型--》
      

  4.   

            // System.IO.FileInfo fileExcel = new System.IO.FileInfo(serverPath);
            int a = serverPath.LastIndexOf("\\") + 1;
            string fileName = serverPath.Substring(a, serverPath.Length - a);        System.IO.FileStream fs = new System.IO.FileStream(serverPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            byte[] bFile = new byte[fs.Length];
            System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
            bFile = r.ReadBytes((int)fs.Length);
            r.Close();
            r = null;
            fs.Close();        Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "online ;filename=" + Server.UrlEncode(fileName)); //把 attachment 改为 online 则在线打开
            //Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + Server.UrlEncode(fileName) + "\"");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//utf-8
            Response.ContentType = "application/ms-excel";
            Page.EnableViewState = false;
            Response.BinaryWrite(bFile);
            Response.Flush();
            Response.End();
      

  5.   

    attachment 提示
     online 在线打开