HTML5 upload attribute not working formatting file rename on upload

I have been trying for several weeks to change the name of the uploaded file. But so far no luck. Instead of a filename, Chrysanthemum.jpg

it becomes the hash of the file, i.e.241693260.jpg

In my back-end, I am using Node.js and Express.js to handle file upload / download. I am using filename and attributes to make sure no files are overwritten.

Here is the HTML code:

<a class="btn btn-info" href="http://localhost:3000/getAttachments?name=Chrysanthemum.jpg&amp;type=image%2Fjpeg&amp;size=879394&amp;lastModified=1247549551674&amp;extension=jpg" download="Chrysanthemum.jpg" target="_blank">Download File</a>

      

Here's my back-end:

app.use('/uploads', express.static(__dirname + "/uploads"));

app.get("/getAttachments", function (req, res) {
    try {
        var fileToBeSent = hashCode(req.query.name + req.query.type + req.query.size + req.query.lastModified + req.query.extension);
        fileToBeSent += req.query.extension  ? '.' + req.query.extension : '';

    // res.sendFile("./uploads/" + fileToBeSent, {
    //     root: __dirname,
    //     "Content-Disposition": '"attachment; filename="' + (req.query.extension  ? '.' + req.query.extension : '') + '"',
    //     "Content-Type": "application/octet-stream"
    // });

        res.redirect("/uploads/" + fileToBeSent);
    } catch (err) {
        console.log("an attempt to GET a file that doesn't exist.");
    }
});

      

Since I am renaming the file name in my back-end , I am trying to rename the file back to its original name using HTML5, but I am failing.

UPDATE . Using express sendFile expression in the same problem.

UPDATE . My server is using cross origin

+3


source to share


3 answers


All you have to do is provide the full name of the file you want to upload the file to. In your Content-Disposition, it doesn't look like you are providing the full filename.



res.sendFile(__dirname + "/uploads/" + fileToBeSent, {
        headers: {
            'Content-Disposition': 'attachment; filename="' + req.query.name + '"'
        }
    });

      

+2


source


I ran into this problem a year ago and wasted a lot of time before I discovered it was a potential Chrome problem Prove for version 35 and later Avoid repeating names, you can use an incremental static identifier. Hope this answer is helpful



+2


source


Renaming the file again with Node JS rather than HTML5 is your best bet.

Renaming files with node.js

0


source







All Articles