zoukankan      html  css  js  c++  java
  • FileUpload文件上传

    一、数据想上传到SqlServer中 数据库的类型设置成 varbinary(MAX) 二进制

    byte[]上传数据时变成二进制进行存储

    byte[] bData = File.ReadAllBytes(sFileName);
    string sql = "insert into BPS_BarcodeTemplate(Template,DataSource,CreatedDate,CreatedBy,Name,ProjectId) values(@Template,@DataSource,getdate(),@UserName,@Name,@ProjectId)";
    int iExeRow = ExecuteNonQuery(sql, CommandType.Text, RunningConfig.BDebug, RunningConfig.SMachineId, new SqlParameter[]{
    new SqlParameter("@Template",data),
    new SqlParameter("@DataSource",sDataSource),
    new SqlParameter("@UserName",sUserName),
    new SqlParameter("@Name",sFileName),
    new SqlParameter("@ProjectId",iProjectId),
    });

    public static int ExecuteNonQuery(string sql, System.Data.CommandType ctType, params SqlParameter[] pms)
    {
    using (SqlConnection conn = new SqlConnection(SSqlConfig))
    {
    int count = 0;
    SqlCommand sqlCommand = null;
    try
    {
    sqlCommand = conn.CreateCommand();
    sqlCommand.CommandText = sql;
    if (pms != null && pms.Length > 0)
    {
    sqlCommand.Parameters.AddRange(pms);
    }
    sqlCommand.CommandType = ctType;
    conn.Open();
    count = sqlCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    throw new Exception(ex.Message);
    }
    finally
    {
    sqlCommand.Dispose();
    }
    return count;
    }
    }

     

    二、上传文件 放在文件bin里面的文件夹中

    /// <summary>
    /// 上传数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button4_Click(object sender, EventArgs e)
    {
    #region 测试
    //OpenFileDialog FileDialog = new OpenFileDialog();
    //FileDialog.Filter = ""; //设置打开类型,设置个*.*和*.txt就行了
    //FileDialog.ShowDialog();
    //this.txtFile.Text = FileDialog.FileName;
    #endregion
    #region 测试
    /*
    //创建一个对话框对象
    OpenFileDialog ofd = new OpenFileDialog();
    //为对话框设置标题
    ofd.Title = "请选择上传的文件";
    //设置筛选的图片格式
    ofd.Filter = "文件格式(*.docx.xlsx.pptx)|*.pptx;*.xlsx;*.docx";
    //ofd.Filter = "文件格式(*.Doc.xls.pdf)|*.Doc;*.xls;*.pdf";

    //设置是否允许多选
    ofd.Multiselect = false;
    //如果你点了“确定”按钮
    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    //获得文件的完整路径(包括名字后后缀)
    string filePath = ofd.FileName;
    //将文件路径显示在文本框中
    txtFile.Text = filePath;
    //找到文件名比如“1.jpg”前面的那个“”的位置
    int position = filePath.LastIndexOf("\");
    //从完整路径中截取出来文件名“1.jpg”
    string fileName = filePath.Substring(position + 1);
    //读取选择的文件,返回一个流
    using (Stream stream = ofd.OpenFile())
    {
    //创建一个流,用来写入得到的文件流(注意:创建一个名为“Images”的文件夹,如果是用相对路径,必须在这个程序的Degug目录下创建
    //如果是绝对路径,放在那里都行,我用的是相对路径)
    using (FileStream fs = new FileStream(filePath, FileMode.CreateNew))
    {
    //将得到的文件流复制到写入流中
    stream.CopyTo(fs);
    //将写入流中的数据写入到文件中
    fs.Flush();
    }
    //PictrueBOx 显示该图片,此时这个图片已经被复制了一份在Images文件夹下,就相当于上传
    //至于上传到别的地方你再更改思路就行,这里只是演示过程
    // pbShow.ImageLocation = @"./Images/" + fileName;
    }
    }*/

    #endregion

    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "文件格式(*.docx.xlsx.pptx)|*.pptx;*.xlsx;*.docx;*.xls;*.Doc;*.pdf";
    // ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files(*.*)|*.*";
    if (ofd.ShowDialog() == DialogResult.OK)
    {
    string extension = Path.GetExtension(ofd.FileName);
    byte[] Datas = File.ReadAllBytes(ofd.FileName);
    string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + extension;
    File.Copy(ofd.FileName, Application.StartupPath + "\Fileupload\" + filename);

    //设置是否允许多选
    ofd.Multiselect = false;
    FileName.Text = "/Fileupload/" + filename;
    this.txtFile.Text = ofd.FileName;
    }
    }

     

    查看文件

    if (lvTougao.SelectedItems.Count > 0)
    {
    var tid = lvTougao.SelectedItems[0].SubItems[0].Text;

    int id = int.Parse(tid);
    var tougao = T_TougaoS.GetById(id);
    try
    {
    System.Diagnostics.Process.Start(Application.StartupPath + tougao.T_File);
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

     

  • 相关阅读:
    结对-结对编项目贪吃蛇-开发环境搭建过程
    gitbook serve运行报错TypeError: cb.apply is not a function
    iOS 工程添加的framework转成pod形式加入
    selector not recognized
    Errors were encountered while preparing your device for development. Please check the Devices and Simulators Window.
    podspec 添加xcassets
    后缀自动机(SAM)构造实现过程演示+习题集锦
    数组中存在undefined,0,null,false等的情况该如何去除
    Uncaught TypeError: date.clone is not a function 【报错解决】
    React·前端URL参数丢失符号的解决办法
  • 原文地址:https://www.cnblogs.com/linnew/p/13491598.html
Copyright © 2011-2022 走看看