angularjs - 当我使用 AngularJS 和 Node.JS 上传文件时出现 "NetworkError: 404 Not Found"

标签 angularjs node.js ng-file-upload

当我想上传文件时遇到此错误:

"NetworkError: 404 Not Found - http://127.0.0.1:8080/src/img"

404: Not found

这是我的页面 View 的代码:

           <div class="col-sm-10">
              <input type="file" ngf-select="uploadFiles($file)"
                     accept="image/*" ngf-max-height="1000" ngf-max-size="1MB">
            </div>

我正在使用ngFileUpload module

我的 Controller :

$scope.uploadFiles = function(file){
  if (file) {
     file.upload = Upload.upload({
         url: 'img',
         data: {file: file}
     });

     file.upload.then(function (response) {
     $timeout(function () {
         console.log(response.data);
     });
     }, function (response) {
     if (response.status > 0)
         console.log(response.status + ': ' + response.data);
  });
 }
}

当我测试链接时:http://127.0.0.1:8080/src/img在我的浏览器中工作正常,但在我的应用程序中不起作用,并且出现上述错误。

我做错了什么?

最佳答案

您可以使用Multer npm 模块用于在 Node.js 中上传文件。如果您使用express 进行路由,那将非常容易。它可以用作中间件。

来自他们的网站:

var express = require('express')
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })

var app = express()

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file 
  // req.body will hold the text fields, if there were any 
})

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files 
  // req.body will contain the text fields, if there were any 
})

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files 
  // 
  // e.g. 
  //  req.files['avatar'][0] -> File 
  //  req.files['gallery'] -> Array 
  // 
  // req.body will contain the text fields, if there were any 
})

文件将以长而奇怪的文件名保存在上传目录中,但您可以使用 req 对象访问原始文件名,例如用于单个文件上传的 req.file.originalname 。然后,您可以使用 fs.readFile 和 fs.writeFile 将该文件移动到具有 acutall 文件名的所需位置。

关于angularjs - 当我使用 AngularJS 和 Node.JS 上传文件时出现 "NetworkError: 404 Not Found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37119397/

相关文章:

node.js - (bluebird) promise 中是否会出现无声错误?

javascript - 使用文本转语音消息发起调用

javascript - Angular ng-file-upload 删除

AngularJS : How do I bind ng-model to an array inside a ng-repeat

javascript - 条件 : Replacing a value with a glyphicon

javascript - 将 DateTime 转换为本地时间

node.js 中的 Python-Shell 未在 Systemd 服务中运行

angularjs - 如何使用 ng-file-upload 在express js中保存文件图像

javascript - AngularJs - 保存在数据库中时减小图像大小

javascript - 取消 ng-change 事件