Guest User

Untitled

a guest
Apr 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. $('#upload-input').on('change', function () {
  2.  
  3. var files = $(this).get(0).files;
  4.  
  5. if (files.length > 0) {
  6. // One or more files selected, process the file upload
  7. var form = new FormData();
  8.  
  9. for (var index = 0; index < files.length; index++) {
  10.  
  11. var file = files[index];
  12. form.append('Uploded Files', file, file.name);
  13. }
  14.  
  15. $.ajax({
  16. url: 'api/fileupload/upload',
  17. type: 'POST',
  18. data: form,
  19. processData: false,
  20. contentType: false,
  21. success: function (data) {
  22. console.log('upload successful!');
  23. }
  24. });
  25. }
  26.  
  27. });
  28.  
  29. FileUpload.remoteMethod
  30.  
  31. (
  32. 'upload', {
  33. http: {
  34. verb: 'post',
  35. },
  36. accepts:
  37. [
  38. { arg: 'ctx', type: 'object', http: { source: 'context' } },
  39. { arg: 'options', type: 'object', http: { source: 'query' } }
  40.  
  41. ],
  42. returns: {
  43. arg: 'data',
  44. type: 'string',
  45. root: true
  46. }
  47. }
  48. );
  49.  
  50. FileUpload.upload = function (context, options, callback) {
  51. //context.req.params = 'common';
  52.  
  53.  
  54. };
  55.  
  56. npm install --save multer
  57.  
  58. var multer = require('multer');
  59. var fs = require('fs');
  60.  
  61. module.exports = function (MyModel) {
  62. var uploadedFileName = '';
  63. var storage = multer.diskStorage({
  64. destination: function (req, file, cb) {
  65. // checking and creating uploads folder where files will be uploaded
  66. var dirPath = 'client/uploads/'
  67. if (!fs.existsSync(dirPath)) {
  68. var dir = fs.mkdirSync(dirPath);
  69. }
  70. cb(null, dirPath + '/');
  71. },
  72. filename: function (req, file, cb) {
  73. // file will be accessible in `file` variable
  74. var ext = file.originalname.substring(file.originalname.lastIndexOf("."));
  75. var fileName = Date.now() + ext;
  76. uploadedFileName = fileName;
  77. cb(null, fileName);
  78. }
  79. });
  80.  
  81.  
  82. MyModel.upload = function (req, res, cb) {
  83. var upload = multer({
  84. storage: storage
  85. }).array('file', 12);
  86. upload(req, res, function (err) {
  87. if (err) {
  88. // An error occurred when uploading
  89. res.json(err);
  90. }
  91. res.json(uploadedFileName);
  92. });
  93. };
  94.  
  95. MyModel.remoteMethod('upload',   {
  96. accepts: [{
  97. arg: 'req',
  98. type: 'object',
  99. http: {
  100. source: 'req'
  101. }
  102. }, {
  103. arg: 'res',
  104. type: 'object',
  105. http: {
  106. source: 'res'
  107. }
  108. }],
  109. returns: {
  110. arg: 'result',
  111. type: 'string'
  112. }
  113. });
  114. };
  115.  
  116. FileUpload.remoteMethod(
  117. 'upload', {
  118. http: {
  119. verb: 'post',
  120. },
  121. accepts:
  122. [{
  123. arg: 'req',
  124. type: 'object',
  125. http: {
  126. source: 'req'
  127. }
  128. }, {
  129. arg: 'res',
  130. type: 'object',
  131. http: {
  132. source: 'res'
  133. }
  134. }],
  135. returns: {
  136. arg: 'data',
  137. type: 'string',
  138. root: true
  139. }
  140. }
  141. );
  142.  
  143.  
  144.  
  145. var uploadedFileName = '';
  146.  
  147. var storage = multer.diskStorage({
  148. destination: function (req, file, cb) {
  149. // checking and creating uploads folder where files will be uploaded
  150. var dirPath = 'client/uploads/'
  151. if (!fs.existsSync(dirPath)) {
  152. var dir = fs.mkdirSync(dirPath);
  153. }
  154. cb(null, dirPath + '/');
  155. },
  156. filename: function (req, file, cb) {
  157. // file will be accessible in `file` variable
  158. console.log("----------Second Rakesh---");
  159. console.log(file);
  160. var ext = file.originalname.substring(file.originalname.lastIndexOf("."));
  161. var fileName = Date.now() + ext;
  162. uploadedFileName = fileName;
  163. cb(null, fileName);
  164. }
  165. });
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. FileUpload.upload = function (req, res, callback) {
  173.  
  174. var upload = multer({
  175. storage: storage
  176. }).array('file', 12);
  177.  
  178. upload(req, res, function (err) {
  179. if (err) {
  180. // An error occurred when uploading
  181. res.json(err);
  182. }
  183. console.log("-------------Rakesh"); // Its Printing Rakesh
  184.  
  185. res.json(uploadedFileName);
  186. });
  187. };
  188.  
  189. $ lb datasource
  190. [?] Enter the data-source name: myfilesystem
  191. [?] Select the connector for myfilesystem: other
  192. [?] Enter the connector name: loopback-component-storage
  193. [?] Install storage (Y/n)
Add Comment
Please, Sign In to add comment