我创建了一个简单的文件上传应用程序与回送。 应用程序客户端我使用简单的html和Java脚本代码。文件上传与回送
我打电话与AJAX调用回送的API,这是Java脚本代码 -
$('#upload-input').on('change', function() {
var files = $(this).get(0).files;
if (files.length > 0) {
// One or more files selected, process the file upload
var form = new FormData();
for (var index = 0; index < files.length; index++) {
var file = files[index];
form.append('Uploded Files', file, file.name);
}
$.ajax({
url: 'api/fileupload/upload',
type: 'POST',
data: form,
processData: false,
contentType: false,
success: function (data) {
console.log('upload successful!');
}
});
}
});
但我们不会在服务器端获取文件。在服务器端,我们创建了一个Loopback API。
可以帮助我们,如何上传带有loopback API的文件。
这是我回送API代码 -
FileUpload.remoteMethod
(
'upload', {
http: {
verb: 'post',
},
accepts:
[
{ arg: 'ctx', type: 'object', http: { source: 'context' } },
{ arg: 'options', type: 'object', http: { source: 'query' } }
],
returns: {
arg: 'data',
type: 'string',
root: true
}
}
);
FileUpload.upload = function (context, options, callback) {
//context.req.params = 'common';
};
是否检查了loopback-storage-component - http://loopback.io/doc/en/lb3/Storage-component.html – Aks1357
是的,但是我们怎么用它!请澄清 – Rakesh
谢谢,我们需要将文件存储在Server端而不是Container中。 – Rakesh