Multer: File Upload works with http but not with https

Created on 20 Oct 2016  路  5Comments  路  Source: expressjs/multer

I am trying to upload a text file to the server using the node js application. When tested through http request the upload was successful and was visible in the POST data.
But now have moved the application to development env and accessing through https url. Now its not showing the details in the POST data and the upload is not working stating file not found.

Can please someone help me out with this

Most helpful comment

@RogerPaladin I run your code and it was returning this error when uploading a file:
Error: ENOENT: no such file or directory, open 'tmp/photo-1474488201814-b5bc1b20ad84.jpg'
I fixed it adding a slash in destination path. Previously It was: "tmp/" and I changed it to "/tmp/".

To run your code in https on my machine I followed this quick tutorial: http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/.

Gist to my server file: https://gist.github.com/flowck/a4f66ed45ba9fea419597c8d81a5500a

I am using:

Ubuntu 16.04
Nodejs v6.9.0
NPM: 3.10.8

screenshot from 2016-10-27 21-12-02

All 5 comments

Same thing

Hi @Kumaravel2904 @RogerPaladin, would be nice if you post a snippet of your code here.

Express

var multer = require('multer');

var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, 'tmp/');
    },
    filename: function(req, file, cb) {
        cb(null, file.originalname);
    }
});

var upload = multer({
    storage: storage
});

webServer.post('/upload/file', upload.single("upload"), function(req, res) {
    if (req.file) {
        res.send("Upload complete");
        res.end();
    } else {
        res.send("No file");
        res.end();
    }
});

Html

<form enctype="multipart/form-data"><input type="file" name="upload" />
    <br/>
    <button class="btn btn-primary" type="button">Send</button>
</form>
<script>
    var file;
    $(':button').click(function() {
        var formData = new FormData($('form')[0]);
        $.ajax({
            url: 'upload/file',
            type: 'POST',
            xhr: function() { // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) { // Check if upload property exists
                    //myXhr.upload.addEventListener('progress', progressHandlingFunction, false); // For handling the progress of the upload
                }
                return myXhr;
            },
            //Ajax events
            beforeSend: function() {
                if (!file) {
                    alert("No file");
                    return false;
                }
            },
            success: function(e) {
                alert(e)
            },
            error: function(e) {
                alert(e)
            },
            // Form data
            data: formData,
            //Options to tell jQuery not to process data or worry about content-type.
            cache: false,
            contentType: false,
            processData: false
        });
    });

    $(':file').change(function() {
        file = this.files;
    });
</script>

Ohhh, it's my fault, mistake in jquery syntax - need to assign id to form
<form enctype="multipart/form-data" id="upload">
And then get FormData from it.
var formData = new FormData($('#upload')[0]);

@RogerPaladin I run your code and it was returning this error when uploading a file:
Error: ENOENT: no such file or directory, open 'tmp/photo-1474488201814-b5bc1b20ad84.jpg'
I fixed it adding a slash in destination path. Previously It was: "tmp/" and I changed it to "/tmp/".

To run your code in https on my machine I followed this quick tutorial: http://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/.

Gist to my server file: https://gist.github.com/flowck/a4f66ed45ba9fea419597c8d81a5500a

I am using:

Ubuntu 16.04
Nodejs v6.9.0
NPM: 3.10.8

screenshot from 2016-10-27 21-12-02

Was this page helpful?
0 / 5 - 0 ratings

Related issues

karandutt01 picture karandutt01  路  3Comments

josephstgh picture josephstgh  路  3Comments

Paul-Morris picture Paul-Morris  路  3Comments

donjae picture donjae  路  4Comments

sant123 picture sant123  路  4Comments