Home > Blockchain >  Failed to upload image file in post man
Failed to upload image file in post man

Time:08-16

//I have already on allow reading files outside working directory in postman and also change my image file path from desktop to the postman user file but it does not work

//answer-->answer

//error screenshots--> cmd

cmd prompt error screenshot

postman error screenshot

 const formidable=require("formidable");
    const {errorHandler}=require('../helpers/dbErrorHandler');
    const _=require("lodash");
    const fs=require('fs');
    const Product=require("../models/product");
    
    
    exports.create=(req,res)=>{
     let form = new formidable.IncomingForm();//IncomingForm is a method of Formidable package and form data sent from react/postman
     form.keepExtensions=true;//whatever image type we getting extenion will be there
      form.parse(req,(err,fields,files)=>{
        if(err)
        {
            return res.status(400).json({
                error:"image could not be uploaded"
            })
        }
        let product=new Product(fields);//fields-->like name,description etc
        if(files.photo)//for photo & files.photo means user sent photo
        {
            product.photo.data=fs.readFileSync(files.photo.filepath);
            product.photo.contentType=files.photo.mimetype;
        }
        product.save((err,result)=>{
            if(err)
            {
                return res.status(400).json({
                    error:errorHandler(err)
                })
            }
            res.json(result);
        })
    })
    };

CodePudding user response:

To sum up. The solution was to set the content-type header in postman request to multipart/form-data

  • Related