zoukankan      html  css  js  c++  java
  • sql 将文件转换为二进制上传(或下载)到数据库中

    附件表结构如下:

    上传附件:

            /// <summary>
            /// 上传附件
            /// </summary>
            /// <param name="filePath">上传文件路径</param>
            /// <param name="strAttachmentId">返回附件ID</param>
            /// <returns>true :上传成功  ; false :上传失败</returns>

    private bool UploadAttachment(string filePath, out string strAttachmentId)
            {

                strAttachmentId = null;
                if (string.IsNullOrEmpty(filePath))
                {
                    return false;
                }
                //截取路径下的文件名
                int index = filePath.LastIndexOf(@"\");
                string fileName = string.Empty;
                if (index > 0)
                {
                    fileName = filePath.Substring(index + 1, filePath.Length - index - 1);
                }
                //将附件内容转换成二进制信息
                FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                byte[] bufContent = new byte[stream.Length];
                stream.Read(bufContent, 0, Convert.ToInt32(stream.Length));
                stream.Flush();
                stream.Close();

              

               //三层架构 访问数据库代码

                DBAccess.Model.attachment model = new DBAccess.Model.attachment();
                strAttachmentId = Guid.NewGuid().ToString("D");
                model.Id = strAttachmentId;
                model.Name = fileName;
                model.Content = bufContent;
                model.UploadTime = DateTime.Now;

                DBAccess.BLL.attachment bll = new DBAccess.BLL.attachment();
                if (bll.Add(model) <= 0)
                {
                    strAttachmentId = null;
                    return false;
                }
                return true;
            }

     

     

    下载附件:

            /// <summary>
            /// 下载附件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnDownload_Click(object sender, RoutedEventArgs e)
            {
                if (string.IsNullOrEmpty(tbAttachment.Text))
                {
                    return;
                }

                //保存对话框
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Title = "下载附件";
                saveFileDialog.Filter = "所有文件|*.*";
                saveFileDialog.FileName = tbAttachment.Text.Trim();
                saveFileDialog.RestoreDirectory = true;
                if ((bool)saveFileDialog.ShowDialog().GetValueOrDefault())
                {

                    //三层架构 访问数据库代码
                    DBAccess.BLL.attachment aBll = new DBAccess.BLL.attachment();
                    DBAccess.Model.attachment modelAt = aBll.GetModel(m_ModelProblemorder.AttachmentId);

                    //将数据库中的二进制转换为文件
                    FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write);
                    fs.Write(modelAt.Content, 0, modelAt.Content.Length);
                    fs.Flush();
                    fs.Close();
                    MessageBox.Show("下载成功!", "提示信息");
                }
            }

     

     

        以上代码是上传,下载附件的主要代码,仅供参考~

  • 相关阅读:
    25.Zabbix入门必备
    6.Ansible Roles角色实战
    5.Ansible Jinja2 模板
    4.Ansible Task控制
    3.Ansible varialbes实战
    2.Ansible Playbook剧本
    1.Ansible自动化管理工具
    网站架构面试题必备
    winsows CMD及Linux命令大全 欢迎补充
    Oracle查询表空间
  • 原文地址:https://www.cnblogs.com/luohengstudy/p/3083750.html
Copyright © 2011-2022 走看看