Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var multer = require('multer');
- //Muler manager
- var storage = multer.diskStorage({
- destination: 'public/uploads/',
- filename: function (req, file, cb) {
- cb(null, Date.now()+"-"+file.originalname)
- }
- });
- var upload = multer({ storage: storage })
- app.use(upload.array('files'));
- router.post('/upload', function(req, res, next) {
- //We check the content
- if(req.files){
- //temporary files array
- var files= [];
- var i = 0,
- len = req.files.length;
- req.files.forEach(function (file, index) {
- i++;
- let file = {
- name : file.filename,
- size : file.size,
- type : file.type,
- path : file.path
- }
- files.push(file);
- if(i == len){
- res.send(files);
- }
- })
- }else{
- res.send("Any file uploaded";);
- }
- })
- @html.extend('layout', function(model){
- @html.block('renderbody', function(model){
- <div class="">
- <div class="row">
- <div class="col-xs-12">
- <a class="upload-btn">UPLOAD</a>
- <div class="progress">
- <div class="progress-bar" role="progressbar"></div>
- </div>
- <input id="fichiers" type="file" name="fichiers" style="display: none" /><br>
- <ul id="image-list">
- </ul>
- </div>
- </div>
- </div>
- })
- })
- $('.upload-btn').on('click', function () {
- $('#fichiers').click();
- var input = document.getElementById("fichiers");
- input.addEventListener('change', function(){
- var formData = new FormData(),
- file,
- reader,
- sortie = false;
- if(input.files.length > 0){
- file = input.files[0];
- sortie = true;
- }
- //And we add the file to formData object
- //This last will have key "files" And value "the file"
- formData.append('files', file, file.name);
- //We check the get out condition
- if(true){
- //On exécute la requête ajax
- $.ajax({
- url : '/upload',
- type : 'POST',
- data : formData,
- processData: false, // tell jQuery not to process the data
- contentType: false, // tell jQuery not to set contentType
- success : function(data) {
- console.log(data);
- }
- });
- }
- input.value="";
- })
- });
Add Comment
Please, Sign In to add comment