我有个有趣的问题。我正试图上传一份表格
<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,使其包括
"storage": {
"name": "storage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./server/storage"
}还是一无所获。
谢谢
发布于 2015-08-25 17:32:31
因此,不幸的是,在讨论如何上传文件时,文档非常有限。有一个参考模块“回环-组件-存储”,一个人必须撕碎它,以找到这个钻石在毛坯。
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 这里的文档。
发布于 2016-12-15 13:06:16
谢谢你的指导。我得到的文件上传与回环和角度2,其中考虑到文件是多么小,这两个是一个巨大的壮举。一些扩展:
在MyModel.myFunction = function(req, res, options, cb) {中,options并不是绝对必要的。您只需将任何所需的选项作为formData对象的“属性”传递,而该属性很可能传递给req对象。在您的示例中,如果options没有传递到函数中,而是只传递给formData,那么cb将不会被分配,并且会对整个调用进行bork。
如果有人想知道我是如何做到这一点的,主要的要求是:
角2
upload-test.component.ts
let file = event.srcElement.files[0];
let formData:FormData = new FormData();
this.forumPostService.uploadFile(formData).subscribe(
response => {
console.log(response);
}
);upload-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();
});
}保存该文件的"formData“被传递到http请求中的"req”以回送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'}
}
);https://stackoverflow.com/questions/32177592
复制相似问题