我有一个有趣的问题。我正在尝试通过
上传表单<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'}},
]
}
);
不幸的是身体是空白的
如何获取表单数据?我修改了server / datasources.json以包含
"storage": {
"name": "storage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./server/storage"
}
仍然没有。
由于
答案 0 :(得分:5)
所以遗憾的是,在讨论如何上传文件时,文档非常有限。有一个参考模块“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
的文档答案 1 :(得分:0)
感谢您的指导。我有文件上传工作环回和角度2,考虑到这两个文档有多小是一个巨大的壮举。一些扩展:
在MyModel.myFunction = function(req, res, options, cb) {
中,options
并非绝对必要。您可以将任何所需的选项作为&#34;属性&#34;可能会传递给req
对象的formData对象。在您的示例中,如果options
未传递到函数中,而只传递给formData,则不会分配cb
,并且会对整个调用进行bork。
如果有人想知道我是如何实现这一目标的,那么主要的呼叫是:
上传-test.component.ts 强>
let file = event.srcElement.files[0];
let formData:FormData = new FormData();
this.forumPostService.uploadFile(formData).subscribe(
response => {
console.log(response);
}
);
上传-test.service.ts 强>
uploadFile(formData: FormData) {
//LEAVE HEADERS BLANK. LOOPBACK CALL BORKS IF ASSIGNED!
let headers = new Headers();
return this.http
.post('http://apiUrl/myModel/uploadTest',
formData,
{headers: headers})
.map((response: Response) => {
return response.json();
});
}
保存文件的&#34; formData&#34;传递给&#34; req&#34;在http请求中回送API
<强> uploadTest.js 强>
myModel.uploadTest = function(req, res, callback) {
var storage = require('loopback-component-storage');
//"root:'/'" will point to home drive, e.g. 'C://'
var storageService = storage.StorageService({provider:'filesystem', root:'/'});
/*"upload" will give e.g. 'C://upload', make sure this folder exists,
it wont create it for you*/
storageService.upload(req, res, { container: 'upload'}, function(err, data) {
if (err) {
callback(err);
}
else {
console.log(data.files.file[0]);
callback(null, data.files.file[0]);
}
});
}
PostSections.remoteMethod(
'uploadTest',{
accepts: [
{arg: 'req', type: 'object', 'http': {source: 'req'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
],
returns: [
{ arg: 'Result', type: 'object' }
],
http: { path: '/uploadTest', verb: 'post'}
}
);