hi Brian,
I'm trying to upload image to my local disk with code as below, the file is upload properly and data has been insert into my table, the question is when res.redirect('/post') is executing, the home page can not render the image, but if i right click the image broken icon and open it to another chrome tab, it shows good.
can you please help to check?
//upload file to local disk
req.file('imagefile').upload({
// don't allow the total upload size to exceed ~10MB
dirname: '../../assets/images/gallery',
maxBytes: 10000000
},async function (err, uploadedFile) {
if (err) {
return res.serverError(err);
}
// If no files were uploaded, respond with an error.
if (uploadedFile.length === 0){
return res.serverError("No files were uploaded!");
}
// Use this log all the uploaded file info
console.log(uploadedFile[0]);
// Get the name of the file
var fileName = uploadedFile[0].filename;
console.log("filename = " + fileName)
// Get the file descriptor and remove the directory details
var fileUID = uploadedFile[0].fd.replace(/^.*[\\\/]/, '');
fileUID = "http://localhost:1337/images/gallery/" + fileUID
// Create the image in your Database
const userId = req.session.userId
await Post.create({
title: title,
text: postBody,
appraisal: appraisal,
user: userId,
imageUrl: fileUID
}).fetch()
res.redirect('/post')
});
######home.ejs##########
<% allPosts.forEach(p => { %>
<div class="card">
<div style='font-weight: bold'>
<%=p.user.fullName %>
</div>
<div style="color: rgb(186, 186, 186)">
<%= new Date(p.createdAt).toLocaleTimeString() + " " + new Date(p.createdAt).toLocaleDateString() %>
</div>
<div class="textbody">
<%- p.text %>
</div>
<img style="max-width: 400px;" src='<%= p.imageUrl%>' />
</div>
<% }) %>