当我使用AngularJS和Node.JS上传文件时,“NetworkError:404 Not Found”

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

“NetworkError:404 Not Found – http://127.0.0.1:8080/src/img ”

404:找不到

这是我的页面视图的代码:

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

我正在使用ngFileUpload模块

我的控制器:

 $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); }); } } 

当我testing的链接: http : //127.0.0.1 : 8080/src/img在我的浏览器工作正常,但在我的应用程序不起作用,我得到上述错误。

我究竟做错了什么?

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

从他们的网站:

 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 // // eg // req.files['avatar'][0] -> File // req.files['gallery'] -> Array // // req.body will contain the text fields, if there were any }) 

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