"NetworkError: 404 Not Found" when i upload a file using AngularJS and Node.JS
"NetworkError: 404 Not Found" when i upload a file using AngularJS and Node.JS
我在上传文件时遇到了这个错误:
"NetworkError: 404 Not Found - http://127.0.0.1:8080/src/img"
404: Not found
这是我的页面视图代码:
<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
我的控制器:
$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);
});
}
}
当我测试 link 时: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 文件名的所需位置。
我在上传文件时遇到了这个错误:
"NetworkError: 404 Not Found - http://127.0.0.1:8080/src/img"
404: Not found
这是我的页面视图代码:
<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
我的控制器:
$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);
});
}
}
当我测试 link 时: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 文件名的所需位置。