I created a simple file uploading application with loopbackjs. In the application's client side I used simple HTML and Javascript code, calling a loopback api with an AJAX call:
$('#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!');
}
});
}
});
We are not getting files on the server side. On the server side we created a Loopback API, but how can we upload files with this API?
This is my loopback API code:
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';
};