protected void Button2_Click(object sender, EventArgs e)
        {
            if (FileUpload1.FileName == string.Empty)
            {
                TextBox4.Text = "请选择文件";
            }
            else
            { 
                TextBox4.Text = string.Empty;
                MemoryStream pdfStream = new MemoryStream();
                byte[] pdfByte = pdfStream.GetBuffer();
                MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
                conn.Open();                string insertStr = "insert into pdfcontents (pdfcontents) values(?pdfByte)";
                MySqlCommand comm = new MySqlCommand();
                comm.Connection = conn;
                comm.CommandText = insertStr;
                
                comm.Parameters.Add(new MySqlParameter("?pdfByte", MySqlDbType.MediumBlob)).Value = pdfByte;
                try
                {
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                comm.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }
我是有3个控件,一个FileUpload1选择pdf文件,一个button,一个textbox。点击button,FileUpload1为空的时候,textbox4.tex提示请选择,不为空的时候如果存入了数据库则显示textbox4.text“存储成功”。
代码写得好像不对,希望大神帮忙修改下

解决方案 »

  1.   

    将PDF 上传后 ,应该上传到服务器上,你用server.mapPath获取绝对路径,然后再把文件2禁制流处理,然后插入数据库里。你的错误是 上传没获取服务器上的文件 就直接转换 是不行的~
      

  2.   

    你的代码里面的pdfStream现在是个空的 没往流里面写入东西啊 应该把你选择的文件的内容写到pdfStream流里面啊 然后才能处理这个流
      

  3.   

    我对这个问题也感兴趣,只能先帮你ding,期待大神解决了
      

  4.   

    如果是我,会在数据库中保存pdf文件在服务器上的路径。从数据库中取出文件路径,然后直接根据路径去访问文件。
    以前保存图片都是这么做的。
      

  5.   

    现在我是做一个简单的页面,可以选择pdf就省的写路径了
      

  6.   

     protected void ButtonSaveFile_Click(object sender, EventArgs e)
            {
                byte[] fileArr = FileUpload1.FileBytes;            using (SqlConnection con = new SqlConnection(@"Data Source=111GOTOHVM\SQLEXPRESS;Initial Catalog=testDB;Integrated Security=True"))
                {
                    con.Open();
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandText = "insert into FileInfo values(@id,@description,@content)";
                    cmd.CommandType = System.Data.CommandType.Text;                SqlParameter idSp = new SqlParameter("@id", "11");
                    idSp.DbType = System.Data.DbType.String;                SqlParameter deSp = new SqlParameter("@description", "nothing to say");
                    deSp.DbType = System.Data.DbType.String;                SqlParameter conSp = new SqlParameter("@content", fileArr);
                    conSp.DbType = System.Data.DbType.Binary;                cmd.Parameters.Add(idSp);
                    cmd.Parameters.Add(deSp);
                    cmd.Parameters.Add(conSp);                cmd.ExecuteNonQuery();            }        }        protected void ButtonReadFile_Click(object sender, EventArgs e)
            {            DataTable dtResult = new DataTable();
                using (SqlConnection con = new SqlConnection(@"Data Source=111GOTOHVM\SQLEXPRESS;Initial Catalog=testDB;Integrated Security=True"))
                {
                    con.Open();
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandText = "select * from FileInfo";
                    cmd.CommandType = System.Data.CommandType.Text;                SqlDataAdapter sdp = new SqlDataAdapter(cmd);                sdp.Fill(dtResult);                cmd.ExecuteNonQuery();            }            if (dtResult != null && dtResult.Rows.Count > 0)
                {
                    System.IO.File.WriteAllBytes(@"c:\test.txt", (byte[])dtResult.Rows[0][2]);            }
            }
    没有mysql的数据库,sql 的写了个很简单的例子,道理上一样的,就是讲文件的二进制保存到DB中。
    PS:另外还有一种方式就是数据库只记录文件的路径,把文件保存在服务器的文件目录下,然后根据DB的路径来定位文件。
      

  7.   

            protected void Button2_Click(object sender, EventArgs e)
            {
                if (FileUpload1.FileName == string.Empty)
                {
                    TextBox4.Text = "请选择文件";
                }
                else
                {   
                    byte[] pdfByte = FileUpload1.FileBytes;
                    MySqlConnection conn = new MySqlConnection();
                    conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
                    conn.Open();
                    MySqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "insert into pdfcontents (pdfcontents) values(@contents)";
                    cmd.CommandType = System.Data.CommandType.Text;
                    MySqlParameter contents = new MySqlParameter("@contents", pdfByte);
                    contents.DbType = System.Data.DbType.Binary;
                    cmd.Parameters.Add(contents);
                    cmd.ExecuteNonQuery();
                }
            }这样还不行啊,我设了断点,都没用,跳不过去三个控件,一个button2,一个textbox4,一个fileupload1
    fileuplocad1选择pdf文件
    点击button2存入pdf文件到mysql数据库
    如果为空则 textbox4.text=“请选择文件”
    如果不为空则 textbox4.text = “存储成功”
      

  8.   

            protected void Button2_Click(object sender, EventArgs e)
            { 
                    byte[] pdfByte = FileUpload1.FileBytes;
                    MySqlConnection conn = new MySqlConnection();
                    conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
                    conn.Open();
                    MySqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "insert into pdfcontents (pdfcontents) values(@contents)";
                    cmd.CommandType = System.Data.CommandType.Text;
                    MySqlParameter contents = new MySqlParameter("@contents", pdfByte);
                    contents.DbType = System.Data.DbType.Binary;
                    cmd.Parameters.Add(contents);
                    cmd.ExecuteNonQuery();
                    TextBox4.Text = "存储成功";
            }三个控件,一个button2,一个textbox4,一个fileupload1
    fileuplocad1选择pdf文件
    点击button2存入pdf文件到mysql数据库
    如果为空则 textbox4.text=“请选择文件”
    如果不为空则 textbox4.text = “存储成功” 现在这个情况,如果fileuplocad1中选择了文件,点击没反应,如果不选择文件,则触发button2_click()
      

  9.   

    我是想把手动选择的pdf文件转化成二进制流写入数据库
      

  10.   

    原来是pdf文件过大,我说怎么代码起不到作用呢,唉,这个问题居然弄了一天了
      

  11.   

    配置
    <httpRuntime executionTimeout="300" maxRequestLength="999999" useFullyQualifiedRedirectUrl="false"/>mysql数据库也要配置数据包大小
      

  12.   

    我现在存进去了,但是有一个24.1M的存不进去。8.9M的能存进去,mysql最大能存多少的啊?
      

  13.   

    数据类型是longblob类型,存8.9M测试的没问题,但是有20多M的就不行,其实就一个pdf文件,就24M多
      

  14.   

    16楼都给你解决办法了,request的设置大小不够,重新设置大一些就可以了<httpRuntime executionTimeout="300" maxRequestLength="999999" useFullyQualifiedRedirectUrl="false"/>