Guest User

Untitled

a guest
Feb 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. var multer = require('multer');
  2.  
  3. //Muler manager
  4. var storage = multer.diskStorage({
  5. destination: 'public/uploads/',
  6. filename: function (req, file, cb) {
  7. cb(null, Date.now()+"-"+file.originalname)
  8. }
  9. });
  10.  
  11. var upload = multer({ storage: storage })
  12. app.use(upload.array('files'));
  13.  
  14. router.post('/upload', function(req, res, next) {
  15.  
  16. //We check the content
  17. if(req.files){
  18.  
  19. //temporary files array
  20. var files= [];
  21.  
  22. var i = 0,
  23. len = req.files.length;
  24.  
  25. req.files.forEach(function (file, index) {
  26.  
  27. i++;
  28.  
  29. let file = {
  30. name : file.filename,
  31. size : file.size,
  32. type : file.type,
  33. path : file.path
  34. }
  35.  
  36. files.push(file);
  37.  
  38. if(i == len){
  39. res.send(files);
  40. }
  41. })
  42.  
  43. }else{
  44. res.send("Any file uploaded";);
  45. }
  46.  
  47. })
  48.  
  49. @html.extend('layout', function(model){
  50. @html.block('renderbody', function(model){
  51. <div class="">
  52. <div class="row">
  53. <div class="col-xs-12">
  54. <a class="upload-btn">UPLOAD</a>
  55. <div class="progress">
  56. <div class="progress-bar" role="progressbar"></div>
  57. </div>
  58. <input id="fichiers" type="file" name="fichiers" style="display: none" /><br>
  59. <ul id="image-list">
  60. </ul>
  61. </div>
  62. </div>
  63. </div>
  64. })
  65. })
  66.  
  67. $('.upload-btn').on('click', function () {
  68.  
  69.  
  70. $('#fichiers').click();
  71.  
  72. var input = document.getElementById("fichiers");
  73.  
  74. input.addEventListener('change', function(){
  75.  
  76.  
  77. var formData = new FormData(),
  78. file,
  79. reader,
  80. sortie = false;
  81.  
  82. if(input.files.length > 0){
  83.  
  84. file = input.files[0];
  85. sortie = true;
  86. }
  87.  
  88. //And we add the file to formData object
  89. //This last will have key "files" And value "the file"
  90. formData.append('files', file, file.name);
  91.  
  92. //We check the get out condition
  93. if(true){
  94.  
  95. //On exécute la requête ajax
  96. $.ajax({
  97. url : '/upload',
  98. type : 'POST',
  99. data : formData,
  100. processData: false, // tell jQuery not to process the data
  101. contentType: false, // tell jQuery not to set contentType
  102. success : function(data) {
  103.  
  104.  
  105. console.log(data);
  106. }
  107. });
  108. }
  109.  
  110. input.value="";
  111. })
  112.  
  113. });
Add Comment
Please, Sign In to add comment