gpt4 book ai didi

loopbackjs - 使用 enctype ="multipart/form-data"上传表单时的空白数据

转载 作者:行者123 更新时间:2023-12-01 11:30:50 24 4
gpt4 key购买 nike

我有一个有趣的问题。我正在尝试通过

上传表单
<form enctype="multipart/form-data" action="/myendpoint/:id">
<input type="hidden" name="data" value="mydata" />
<input type="file" name="formname" />
</form>

...和我的远程方法调用:

Patient.uploadVideo = function(id, mydata, cb) {
console.log(mydata);
return cb(null, { id: 123 });
};


MyModel.remoteMethod(
'uploadVideo',
{
http: {path: '/:id/recording/:recordingid/videos', verb: 'post'},
accepts: [
{arg: 'id', type: 'string', required: true},
{arg: 'mydata', type: 'object', 'http': {source: 'body'}},
]
}
);

不幸的是 body 是空白的

如何获取表单数据?我修改了 server/datasources.json 以包含

"storage": {
"name": "storage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./server/storage"
}

仍然没有。

谢谢

最佳答案

因此,不幸的是,在讨论如何上传文件时,文档非常有限。有一个对模块“loopback-component-storage”的引用,必须撕开它才能找到这颗未经加工的钻石。

var storage     =   require('loopback-component-storage');

MyModel.myFunction = function(req, res, options, cb) {
var storage = require('loopback-component-storage');
var storageService = storage.StorageService({provider: 'filesystem', root: '/tmp'});

storageService.upload(req, res, { container: 'upload' }, function(err, data) {
console.log(data); // this provides a nice object with all of the variables and data wrt the file that was uploaded
/// ..etc
});
};
MyModel.remoteMethod(
'myFunction',
{
http: {path: '/mypath', verb: 'post'},
accepts: [
{arg: 'req', type: 'object', 'http': {source: 'req'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
],
returns: {arg: 'something', type: 'object'}
}
);

您可以找到 StorageService 的文档 here

关于loopbackjs - 使用 enctype ="multipart/form-data"上传表单时的空白数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32177592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com