【问题标题】:Store file into SQL Server Using React and Node.js使用 React 和 Node.js 将文件存储到 SQL Server
【发布时间】:2020-05-09 23:21:09
【问题描述】:

我遇到了问题。我需要使用 Node.js 作为后端和 React.js 作为前端,但不知道如何将文件存储到 SQL 服务器做这个。实际上,我有使用 Multer 的经验,但我严格需要将文件存储到 SQL DB 中。我试过了,但它只将几个字节存储到数据库中,而不是完整的文件。

另外请告诉我,当用户点击下载时,我们如何访问数据库中的文件并将其下载到浏览器。

注意:我以blob形式访问文件

// Frontend Action

   this.props.postNewAccountAttachment({
      acta_act_key: '598806',
      acta_file_name: this.state.fileName,
      acta_description: this.state.description,
      acta_attachment: this.state.attachment,
   });
//Backend API Services
async postAccountAttachment(attachmentBody) {
    const attachment = await this.models.account_attachments.create(
      attachmentBody
    );
    return attachment;
  }

【问题讨论】:

    标签: sql node.js reactjs blob buffer


    【解决方案1】:

    根据我的知识和经验,您不应该将实际文件存储在 SQLDB 中,而是将文件上传到服务器中的单独文件夹并将文件信息存储到数据库中。

    即(文件名,类型,大小)

    将文件信息存储到数据库中:

     var sql = "INSERT INTO `file`(`name`, `type`, `size`) VALUES ('" + req.file.filename + "', '"+req.file.mimetype+"', '"+req.file.size+"')";
    
     var query = db.query(sql, function(err, result) {
           console.log('File Information Saved');
     });
    

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      相关资源
      最近更新 更多