简体   繁体   English

如何使用node js和express将PDF存储在mysql数据库中

[英]How to Store PDF in mysql database using node js and express

I am trying to set up a learning system for a uni project and i need the students to be able to upload a PDF of their work to a Table called Assessment Submissions and I need to store the data in one of the rows... I have looked all over and I know you have to use the blob typing but apart from that I am scuppered (the column in the table is called fileUpload).我正在尝试为一个 uni 项目建立一个学习系统,我需要学生能够将他们的工作的 PDF 上传到名为 Assessment Submissions 的表中,我需要将数据存储在其中一行中......我已经看遍了,我知道你必须使用 blob 输入,但除此之外我被吓坏了(表中的列称为 fileUpload)。 Any help would be really useful!!任何帮助都会非常有用! I have attached some code that i am working on我附上了一些我正在处理的代码

Assessment Models评估模型

exports.submitAssessment = async (pupilID, AssesmentsID, Submission,myfile) => {
    


    const assessment = { pupilID, AssesmentsID, Submission, fileUploads:myfile}
    var database = await require('../dbConfig');
     await database.query('INSERT INTO AssessmentSubmissions SET ?', assessment)
}

Assessments Controller评估 Controller

exports.submitAssessment = async (req, res) => 
{   
    console.log(req.originalUrl + " POST Received with query: ", session.pupilID, req.params.AssessmentsID, req.body.Submission)
    const submission = await assessments.submitAssessment(session.pupilID, req.params.AssessmentsID, req.body.Submission,req.body.myfile)
    res.send(submission)
}

Assessment Submission View评估提交视图

<form class="" method="POST" action="{{{req.params.AssessmentsID}}}/submit-assessment">
    <label for="Answer">Answer</label><br>
    <textarea name="Submission"></textarea>
    <label for="myfile">Select a file:</label>
    <input type="file" id="myfile" name="myfile"><br><br>
    <input type="submit" />
</form>

Pdf is a file, the best way to store a file is in a file system like an image or a video, you can event compress it as a zip file to gain more storage. Pdf 是一个文件,存储文件的最佳方式是在图像或视频等文件系统中,您可以将其压缩为zip文件以获得更多存储空间。

All you need is to store a reference of the pdf file, like an id or a unique name, in your database and get the file from your file system when ever you need it.您只需在数据库中存储 pdf 文件的引用,如 id 或唯一名称,并在需要时从文件系统中获取文件。

Here is a tutorial I found just by googling "tutorial node js store files"这是我通过谷歌搜索“ tutorial node js store files”找到的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM