Javascript 使用RESTAPI在ExpressJS和AngularJS中上传文件

Javascript 使用RESTAPI在ExpressJS和AngularJS中上传文件,javascript,node.js,angularjs,rest,express,Javascript,Node.js,Angularjs,Rest,Express,我不熟悉AngularJS和Node.js 我想使用RESTAPI、AngularJS和Express.js实现文件(.pdf、.jpg、.doc)上传功能 我试着从中了解一些想法,但我仍然不清楚如何在RESTAPI中使用AngularJS和Express.js上传文件 有人能给我解释一下如何使用REST API上传AngularJS和Express.js中的文件,并提供一些基本示例吗?我最近遇到了一个类似的问题。问题是,angular并没有对输入类型“file”的最佳支持,因为您不能以两种方式

我不熟悉AngularJS和Node.js

我想使用RESTAPI、AngularJS和Express.js实现文件(.pdf、.jpg、.doc)上传功能

我试着从中了解一些想法,但我仍然不清楚如何在RESTAPI中使用AngularJS和Express.js上传文件


有人能给我解释一下如何使用REST API上传AngularJS和Express.js中的文件,并提供一些基本示例吗?

我最近遇到了一个类似的问题。问题是,angular并没有对输入类型“file”的最佳支持,因为您不能以两种方式绑定它。这就是为什么人们为此制定了自定义指令。我用的那个有一些简洁的例子


它被称为

参考文献:
我已使用以下代码解决了此问题:

安格拉斯 HTML
现在我可以将文件发送到我的nodejs服务器,但是如何获取该文件并将其保存到某个目录中呢?
<input type="file" ng-file-select="onFileSelect($files)" multiple>
  $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.
  };
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!");