Open maheshbasnet089 opened 1 year ago
Need more information about the situation. Code?
//.....app /express /cors code
const multer = require("multer") // app.use(cors()); app.use((req, res, next) => { res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Credentials", "true"); res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,DELETE"); res.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"); next(); })
app.use('/uploads',express.static("./uploads"))
// image config
var imgconfig =multer.diskStorage({
destination:(req,file,callback)=>{
callback(null,"uploads/")
},
filename:(req,file,callback)=>{
callback(null,image-${Date.now()}.${file.originalname}
)
}
})
// image filter
const isimage = (req,file,callback)=>{
if(file.mimetype.startsWith("image")){
callback(null,true)
}
else{
callback(null,Error("Only images are allowed!"))
}
}
//image upload var upload =multer ({ storage :imgconfig, limits: {fileSize:'2000000'}, fileFilter:isimage })
///....other routes
app.post('/student',upload.single("photo"), (req, res) => {
console.log(req.body)
const {data} = req.body;
const {filename} = req.file
if(!filename || !data){
res.status(422).json({status: 422,message:"fill all the details!"})
}
try{
db.query(`insert into student (instituteid,studname,rollno,enrollno,class,section,
father_name,mother_name,blood_group,dob,address,pincode,gender,contactno,pic)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
[...JSON.parse(data),filename],
(err, results, fields) => {
if (err) {
return console.log(err)
}
else{
res.status(201).json({status:201,message:"Successfully submitted!!!",data : results })
}
})
}catch(e){
res.status(422).json({status: 422,e})
}
})
I would change the return console.log(err)
code to res.status(500)
and see if that's what holding up the response.
It was the problem with hosting provider thank you anyway
On Tue, 23 Apr 2024 at 1:19 AM Joey Guerra @.***> wrote:
I would change the return console.log(err) code to res.status(500) and see if that's what holding up the response.
— Reply to this email directly, view it on GitHub https://github.com/expressjs/multer/issues/1200#issuecomment-2070776688, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQXRZ3XDVZXELS6NSMBSXVTY6VQ4BAVCNFSM6AAAAAAW3FFGD2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZQG43TMNRYHA . You are receiving this because you authored the thread.Message ID: @.***>
i was unable to post images to cpanel, it works on local machine but not on hosting ,