Javascript post后nodejs fs.writeFile失败
我有一个文件正在裁剪和加载的角度方面。多亏了 我可以在开发者窗口中看到该文件。我的问题似乎站在Nodejs一边 我的问题在nodejs这边。我有文件名,文件正在以新名称复制到tmp文件夹Javascript post后nodejs fs.writeFile失败,javascript,angularjs,node.js,ng-file-upload,Javascript,Angularjs,Node.js,Ng File Upload,我有一个文件正在裁剪和加载的角度方面。多亏了 我可以在开发者窗口中看到该文件。我的问题似乎站在Nodejs一边 我的问题在nodejs这边。我有文件名,文件正在以新名称复制到tmp文件夹 pofPhil3.jpg image/png /tmp/nYCf_p6kI5h4LfMQffNHbRu1.jpg POST /register/user/uploads 200 8.775 ms - 23 Hello World file.name pofPhil3.jpg 我一直在尝试获取该文件并将其复制到
pofPhil3.jpg
image/png
/tmp/nYCf_p6kI5h4LfMQffNHbRu1.jpg
POST /register/user/uploads 200 8.775 ms - 23
Hello World file.name pofPhil3.jpg
我一直在尝试获取该文件并将其复制到另一个文件夹
var multiparty = require('connect-multiparty');
var multipartMiddleware = multiparty();
router.post('/user/uploads', multipartMiddleware, function(req, res) {
var file = req.files.file;
console.log(file.name);
console.log(file.type);
console.log(file.path);
var imageBuffer = decodeBase64Image(req.files.file);
fs.writeFile('/Users/testuser/test_hold_files/' + file.name, imageBuffer, function (err) {
if (err) return console.log(err);
console.log('Hello World file.name' , file.name);
});
res.status(200).jsonp({file: file.name});
});
正在新文件夹中创建并命名该文件。问题是该文件不是jpg,并且是空的
vagrant@vagrant-ubuntu-trusty-64:/Users/testuser/test_hold_files$ ls -alt
total 16
drwxrwxrwx 2 root root 4096 Feb 27 06:26 .
-rw-rw-r-- 1 vagrant vagrant 15 Feb 27 06:26 pofPhil3.jpg
-rw-rw-r-- 1 vagrant vagrant 15 Feb 27 06:16 philipMallCar.jpg
drwxr-xr-x 4 root root 4096 Feb 27 06:08 .
任何帮助都很好…谢谢
菲尔这是我目前正在使用的。我有问题的图像大小不是更小
var formidable = require('formidable'),
util = require('util'),
fs_extra = require('fs-extra');
This is my post to accept images.
router.post('/user/uploads', function (req, res){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
form.on('end', function(fields, files) {
/* Temporary location of our uploaded file */
var temp_path = this.openedFiles[0].path;
/* The file name of the uploaded file */
var file_name = this.openedFiles[0].name;
/* Location where we want to copy the uploaded file */
var new_location = "/Users/testUser/test_hold_files/";
fs_extra.copy(temp_path, new_location + file_name, function(err) {
if (err) {
console.error(err);
} else {
console.log("success!")
}
});
});
});
用ng文件上传、nodejs文件上传、etcI不认为
var imageBuffer=decodeBase64Image(req.files.file)标记您的问题需要code>时,您应该能够保存文件而无需解码编码等。仍在处理此问题。Upload.Upload({url:'',数据:{file:Upload.dataUrltoBlob(dataUrl,picFile.name)},---这个Upload.dataUrltoBlob似乎是我的问题。我不知道如何在节点上创建POST来接受这个BLOB。BLOB与正在发送的请求中的文件相同,因此如果用实际文件替换它,您可能会遇到同样的问题。您解码该文件而不是将其作为流进行管道传输是有原因的吗你一直在努力解决什么问题?这是一个我发现有点效果的示例。我如何将其写成express中的流?如果我没有弄错的话,4.0需要中间件。你能发布一个更好的示例吗?谢谢我在回答中使用了令人敬畏的.IncomingForm。你可以在我的回答中看到我使用了它。如果你有更好的示例,请发布一个。谢谢。有原因吗您正在创建一个临时文件,或者这是一个解决方法?目的只是上传图像->写入文件吗?
var formidable = require('formidable'),
util = require('util'),
fs_extra = require('fs-extra');
This is my post to accept images.
router.post('/user/uploads', function (req, res){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
form.on('end', function(fields, files) {
/* Temporary location of our uploaded file */
var temp_path = this.openedFiles[0].path;
/* The file name of the uploaded file */
var file_name = this.openedFiles[0].name;
/* Location where we want to copy the uploaded file */
var new_location = "/Users/testUser/test_hold_files/";
fs_extra.copy(temp_path, new_location + file_name, function(err) {
if (err) {
console.error(err);
} else {
console.log("success!")
}
});
});
});