question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid Path upon File Upload from 'Postman'

See original GitHub issue

Hi All! I’m trying to upload an image using multer through my Express Server whenever a ‘POST’ request hits my ‘imageUpload’ REST API End Point. File is uploaded successfully but inside ‘path’ property of ‘req.file’, incorrect path is being shown with backslashes.

“path”: “public\\images\\Jellyfish.jpg” (Please refer to image)

Please let me know where I’m going wrong.

I am using multer@1.4.1

const express = require('express');
const bodyParser = require('body-parser');
const authenticate = require('../authenticate');

const multer = require('multer');

const storage = multer.diskStorage({
    destination: (req, file, callback)=>{
        callback(null, 'public/images');
    },
    
    filename: (req, file, callback)=>{
        callback(null, file.originalname)
    }
});

const imageFileFilter = (req, file, callback)=>{
    if(!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)){
        return callback(new Error('You can upload only image files!'), false);
    }
    callback(null, true);
};

const upload = multer({storage: storage, fileFilter: imageFileFilter});

const uploadRouter = express.Router();

uploadRouter.use(bodyParser.json());

uploadRouter.route('/')
    .get(authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next)=>{
        res.statusCode = 403; //Forbidden
        res.end('PUT operation not supported on /imageUpload');
    })
    
    .post(authenticate.verifyUser, authenticate.verifyAdmin, upload.single('imageFile'), (req, res)=>{
        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');
        res.json(req.file); /* req.file object contains the path to the file which can be used by client */
    })
    
    .put(authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next)=>{
        res.statusCode = 403; //Forbidden
        res.end('PUT operation not supported on /imageUpload');
    })

    .delete(authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next)=>{
        res.statusCode = 403; //Forbidden
        res.end('PUT operation not supported on /imageUpload');
    })


module.exports = uploadRouter;

Capture

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rades12340commented, Apr 13, 2019

I have the same problem, have the right path, it works fine when I use a static file, but when I upload an image with Multer it generates a path with backslashes which don’t work(also in Windows). How I can change this backslashes into forwarding slashes, so it can work?

http://localhost:8080/imagesSat%20Apr%2013%202019-batmanoldnew-719929.jpg

This is the path I have in the network tab in chrome. There should be a slash after an images

0reactions
LinusUcommented, Mar 13, 2019

No problem 🍻

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postman file uploading: the file path became missing when I ...
The problem is that the file path became missing once I close the request tab and I try to run through Runner ,...
Read more >
Can't select file at form-data, "Make sure that Postman can ...
I've set the 'working directory' to be a path on my C: (Windows!). When using Postman web and I select a file under...
Read more >
Example: Using REST APIs to Upload to an External Directory ...
This example uses the 11.1.2.3.600 Upload API, which is a simpler non-chunked version. · If the filename contains special characters or has white...
Read more >
File uploads (profile image, simple, resumable uploads)
If the client tries to send data to an invalid file upload path, or it has waited too long since the last successful...
Read more >
HTTP - Developers - Dropbox.com
RPC endpoints are on the api.dropboxapi.com domain. Content-upload endpoints. These endpoints accept file content in the request body, so their arguments are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found