我如何从formData获取内容? [英] How do i get the content from a formData?
问题描述
var fd = new FormData();
fd.append('file',file);
如何从formData获取内容?像文件名和文件?
是这样的: fd.filename()
, fd.getData()
。
或者 fd.get('file')
来检索文件吗?
在将文件附加到我相信的formData对象后,没有办法检索这些文件。
您必须将formData-object发送到某处,然后从req对象或类似的地方获取文件。
在我的例子中(angularJS + nodeJS),我从SO上的一个答案中测试了这个(下面的链接):
Angular:
var fd = new FormData();
fd.append('file',file);
$ http({
method:POST,
url:uploadFile,
data:fd,
withCredentials:true,
headers: {'Content-Type':undefined},
transformRequest:angular.identity
});
节点(expressJS):
app.post('/ uploadFile',function(req,res){
fs.readFile(req.files.file.path,function(err,data){
//对数据(包含文件信息)做
});
});
要查看您可以对文件做什么,请阅读以下内容:
http://nodejs.org/api/fs.html
代码取自:
AngularJS:如何使用多部分表单实现简单的文件上传?
I have created a file into the formData like this:
var fd = new FormData();
fd.append('file', file);
how do i get the content out of the formData? like the filename and the file?
something like this?: fd.filename()
, fd.getData()
.
or fd.get('file')
to retrieve the file back?
There is no way to retrieve the files after you've appended them in to a formData-object I believe.
You'll have to send the formData-object somewhere and then get the files from a req-object or something like that.
In my case (angularJS + nodeJS) I tested this from an answer on SO (link below):
Angular:
var fd = new FormData();
fd.append('file', file);
$http({
method:"POST",
url:"uploadFile",
data: fd,
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
});
Node (expressJS):
app.post('/uploadFile', function(req,res){
fs.readFile(req.files.file.path, function(err, data){
// Do something with the data (which holds the file information)
});
});
To see what you can do with the file, read this: http://nodejs.org/api/fs.html
The code is taken from : AngularJS: how to implement a simple file upload with multipart form?
这篇关于我如何从formData获取内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!