简体   繁体   English

使用REST API在ExpressJS和AngularJS中上传文件

[英]File upload in ExpressJS and AngularJS using the REST API

I am new to AngularJS and Node.js. 我是AngularJS和Node.js的新手。

I want to implement file (.pdf, .jpg, .doc) upload functionality using the REST API, AngularJS and Express.js. 我想使用REST API,AngularJS和Express.js实现文件(.pdf,.jpg,.doc)上传功能。

I have tried to get an idea from Use NodeJS to upload file in an API call but I am still not clear how I can upload files using AngularJS and Express.js in the REST API. 我试图从使用NodeJS中获取一个API调用上传文件的想法,但我仍然不清楚如何使用REST API中的AngularJS和Express.js上传文件。

Can anyone explain to me how to upload files in AngularJS and Express.js using the REST API, with basic examples? 任何人都可以向我解释如何使用REST API在AngularJS和Express.js中上传文件,并提供基本示例吗?

Reference : https://github.com/danialfarid/angular-file-upload 参考: https //github.com/danialfarid/angular-file-upload
I have resolved this problem using following code : 我使用以下代码解决了这个问题:

Angularjs Angularjs

HTML HTML
 <input type="file" ng-file-select="onFileSelect($files)" multiple> 
JS JS
  $scope.onFileSelect = function($files) { //$files: an array of files selected, each file has name, size, and type. for (var i = 0; i < $files.length; i++) { var file = $files[i]; $scope.upload = $upload.upload({ url: 'server/upload/url', //upload.php script, node.js route, or servlet url // method: 'POST' or 'PUT', // headers: {'header-key': 'header-value'}, // withCredentials: true, data: {myObj: $scope.myModelObj}, file: file, // or list of files: $files for html5 only /* set the file formData name ('Content-Desposition'). Default is 'file' */ //fileFormDataName: myFile, //or a list of names for multiple files (html5). /* customize how data is added to formData. See #40#issuecomment-28612000 for sample code */ //formDataAppender: function(formData, key, val){} }).progress(function(evt) { console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total)); }).success(function(data, status, headers, config) { // file is uploaded successfully console.log(data); }); //.error(...) //.then(success, error, progress); //.xhr(function(xhr){xhr.upload.addEventListener(...)})// access and attach any event listener to XMLHttpRequest. } /* alternative way of uploading, send the file binary with the file's content-type. Could be used to upload files to CouchDB, imgur, etc... html5 FileReader is needed. It could also be used to monitor the progress of a normal http post/put request with large data*/ // $scope.upload = $upload.http({...}) see 88#issuecomment-31366487 for sample code. }; 

Nodejs using expressJS Nodejs使用expressJS

 var path = require('path'), fs = require('fs'); var tempPath = req.files.file.path, targetPath = path.resolve('./uploadFiles/' + req.files.file.name); fs.rename(tempPath, targetPath, function(err) { if (err) throw err; console.log("Upload completed!"); 

I've been dealing with a similar problem recently. 我最近一直在处理类似的问题。 Thing is, angular doesn't have the best support for input type "file", because you cannot bind it 2 way. 事实是,angular对输入类型“file”没有最好的支持,因为你无法将它绑定到2路。 That is why people made custom directives for that. 这就是人们为此制定自定义指令的原因。 The one I used has some neat examples. 我使用的那个有一些简洁的例子。

Its called angular-file-upload 它叫做angular-file-upload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM