1

I am trying to build an API which will take in image so used multer for that purpose, I am getting a success for uploading an image but it saves in some weird format not the actual format I tried to save. I ll post the related code here.

in app.js

const multer = require('multer');
app.use(function(req, res, next) { //allow cross origin requests
    res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    res.header("Access-Control-Allow-Origin", "http://localhost:3001");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Credentials", true);
    next();
});


app.use(express.static('../client'));
app.use(bodyParser.json());

app.use(multer({dest:'./angular-src/src/assets/'}).single('file'));

app.use(express.static(path.join(__dirname,'public')));

In the API File

const express = require('express');
const router = express.Router();
const multer = require('multer');
const storage = multer.diskStorage({ //multers disk storage settings
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }
});

const upload = multer({
    storage: storage
}).single('file');



router.post('/upload',function (req,res) {
   upload(req,res,function (err) {
       if (err){
           res.json({success:false});
           return;
       }else{
           // console.log(req.files[0].originalname);
           res.json({success: true, filename: req.file});
       }
   });
});


module.exports = router;

The stored image looks like this enter image description here

The second one which has alpha numeric characters in it.

Image data is something like this

{
    "_type": "Project",
    "_id": "AAAAAAFF+h6SjaM2Hec=",
    "name": "Untitled",
    "ownedElements": [
        {
            "_type": "UMLModel",
            "_id": "AAAAAAFF+qBWK6M3Z8Y=",
            "_parent": {
                "$ref": "AAAAAAFF+h6SjaM2Hec="
            },
            "name": "Model",
            "ownedElements": [
                {
                    "_type": "UMLClassDiagram",
                    "_id": "AAAAAAFF+qBtyKM79qY=",
                    "_parent": {
                        "$ref": "AAAAAAFF+qBWK6M3Z8Y="
                    },
                    "name": "Main",
                    "visible": true,
                    "defaultDiagram": true
                },

This is how I am making a call from postman

enter image description here

12
  • I checked your code its storing image properly with the correct format, can you describe what problem you are facing? Commented Mar 28, 2018 at 22:03
  • I have updated my question Commented Mar 28, 2018 at 22:07
  • Yes. It is the same Commented Mar 28, 2018 at 22:18
  • I am able to open it but the image data is weird. I ll post the image data here itself Commented Mar 28, 2018 at 22:28
  • Yes,this is the data at the destination where the image is being saved Commented Mar 28, 2018 at 22:40

1 Answer 1

1

This issue has been solved, for those who has similar kind off issue. What worked for me was doing

app.use(multer({dest:'./angular-src/src/assets/'}).single('file')); 

in the API class itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.