简体   繁体   中英

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). 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

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.

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.

Here is a tutorial I found just by googling "tutorial node js store files"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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