none
如何下载附件 RRS feed

答案

  • Code Snippet

     

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

        protected void Page_Load(object sender, EventArgs e)
        {
            string strCnn = "Persist Security Info=False;User ID=sa;Password=;Initial Catalog=Example;Server=(local)\\SQL2005;";
            SqlConnection myConnection = new SqlConnection(strCnn);
            SqlCommand command = new SqlCommand("select * from Photos Where StudentId = 1", myConnection);
            myConnection.Open();
            SqlDataReader dr = command.ExecuteReader();
            if (dr.Read())
            {
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=Student1.bmp");
                Response.BinaryWrite((byte[])dr["img"]);
                Response.End();
            }
            dr.Close();
            myConnection.Dispose();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       
        </div>
        </form>
    </body>
    </html>

     

     

    怎么打开取决于

     Response.ContentType = "application/octet-stream";
     Response.AddHeader("Content-Disposition", "attachment; filename=Student1.bmp");

     

    不是看Response

     

     

    2008年9月21日 1:26
    版主

全部回复

  •     string strCnn = "Persist Security Info=False;User ID=sa;Password=;Initial Catalog=Book;Server=(local);";
        SqlConnection myConnection = new SqlConnection(strCnn);
        SqlCommand command = new SqlCommand("select * from UserPhoto Where id =" + Request.QueryString["id"], myConnection);
        myConnection.Open();
        SqlDataReader dr = command.ExecuteReader();
        if (dr.Read())
        {
          Response.Clear();
          Response.ContentType = "application/octet-stream";

          Response.AddHeader("Content-Disposition", "attachment; filename=xxx.jpg");

          Response.BinaryWrite((byte[])dr["Photo"]);

          Response.End();

        }
        dr.Close();
        myConnection.Dispose();

    2008年9月20日 3:04
    版主
  • 你这个Response.BinaryWrite()方法好像是以浏览页的方式打开的;

    但我想下载这个附件,比如弹出一个对话框,选择"保存"或"打开",保存可以选择路径并保存到本机上,

    选打开可以直接打开;

    请问这种方式如何实现?谢谢

    2008年9月21日 1:14
  • Code Snippet

     

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

        protected void Page_Load(object sender, EventArgs e)
        {
            string strCnn = "Persist Security Info=False;User ID=sa;Password=;Initial Catalog=Example;Server=(local)\\SQL2005;";
            SqlConnection myConnection = new SqlConnection(strCnn);
            SqlCommand command = new SqlCommand("select * from Photos Where StudentId = 1", myConnection);
            myConnection.Open();
            SqlDataReader dr = command.ExecuteReader();
            if (dr.Read())
            {
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=Student1.bmp");
                Response.BinaryWrite((byte[])dr["img"]);
                Response.End();
            }
            dr.Close();
            myConnection.Dispose();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       
        </div>
        </form>
    </body>
    </html>

     

     

    怎么打开取决于

     Response.ContentType = "application/octet-stream";
     Response.AddHeader("Content-Disposition", "attachment; filename=Student1.bmp");

     

    不是看Response

     

     

    2008年9月21日 1:26
    版主
  • 谢谢你的回答

    2008年9月21日 2:12