zoukankan      html  css  js  c++  java
  • 上传文件到数据库

    直接上代码:

    前台:托一个asp.net上传控件和一个button按钮

    后台代码:

    protected void StartUpLoadFile(object sender, EventArgs e)
    {
    if (this.Fu_File.HasFile)
    {
    long fileLength = Fu_File.PostedFile.ContentLength;

    byte[] bytes = new byte[fileLength];

    Stream stream = Fu_File.PostedFile.InputStream;

    stream.Read(bytes, 0, bytes.Length);

    //Response.Write(bytes.Length + bytes.ToString());
    //return;

    InsertFileTable(bytes);
    }
    else
    {
    Response.Write("请先选择一个文件!");
    }
    }

    private void InsertFileTable(byte[] bytes)
    {
    using (SqlConnection con = new SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=DbExam10;"))
    {
    con.Open();

    SqlCommand cmd = new SqlCommand("INSERT INTO FileStreamTable (FileBytes) VALUES (@fileBytes)", con);
    cmd.Parameters.Add("@fileBytes", SqlDbType.VarBinary).Value = bytes;

    if (cmd.ExecuteNonQuery() != -1)
    {
    Response.Write("文件上传成功!");
    }

    }
    }

    希望能多多交流,欢迎指正……
  • 相关阅读:
    ios开发系列-准备工作
    tests
    腾讯DBA官方博客开通了,欢迎交流
    腾讯DBA官方博客开通了
    [HNOI2008]水平可见直线
    BZOJ-4518 征途
    CDQ分治与整体二分
    HYSBZ-1176 Mokia
    二逼平衡树
    可持久化数组
  • 原文地址:https://www.cnblogs.com/zhzhjieke/p/3584485.html
Copyright © 2011-2022 走看看