Skip to content

Commit

Permalink
Append random number to uploaded file names to avoid overwriting files
Browse files Browse the repository at this point in the history
  • Loading branch information
Nateowami committed Dec 13, 2015
1 parent fa85847 commit a8b7fd7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ app.use(require("multer")(
{
dest: __dirname + '/public/uploads',
rename: function(fieldname, filename) {
return filename;
// Append a random number to the file name to avoid overwriting
// files with the same name. This is especially important since some
// devices (such as the iPad) name all images image.jpeg, even when
// multiple images are uploaded with one HTTP request.
return filename + "-" + Math.floor(Math.random()*1000000000);
}
}));
app.use(require("body-parser").json());
Expand Down
5 changes: 2 additions & 3 deletions views/upload.jade
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ block content

p Select files or paste text into the textbox (or both). Then click the upload button.

form(action="/upload" method="post" enctype="multipart/form-data")
form(action="/upload", method="post", enctype="multipart/form-data")

h2 Text upload
textarea(rows="10", name="text")

h2 File upload
p Warning: If you upload two files with the same name, the second will overwrite the first!
input(type="file" name="files" multiple)
input(type="file", name="files", multiple)

h2 Upload it

Expand Down

0 comments on commit a8b7fd7

Please sign in to comment.