• 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("下载成功!", "提示信息");
                }
            }

     

     

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

  • 相关阅读:
    第六周Java学习总结
    结对编程练习_四则运算(一)
    实验一Java开发环境的熟悉
    第四周Java学习总结
    第三周Java学习总结
    2019-2020-2 20175230滕星《网络对抗技术》Exp9 Web安全基础
    2019-2020-2 20175230 滕星《网络对抗技术》Exp 8 Web基础
    2019-2020-2 20175230滕星《网络对抗技术》Exp7 网络欺诈防范
    2019-2020-2 网络对抗技术 20175230滕星 Exp6 MSF基础应用
    2019-2020-2 网络对抗技术 20175230滕星 Exp5 信息搜集与漏洞扫描
  • 原文地址:https://www.cnblogs.com/luohengstudy/p/3083750.html
Copyright © 2020-2023  润新知