on 2020 Nov 11 9:11 AM
Hi,
I am trying to implement an endpoint that allows me to upload files from the browser.
Here is a sample fetch request from the client(browser)
const files =event.getParameter('files');
const file= files[0];
const formData =new FormData();
formData.append(file.name,file);
fetch({
url:`/item/upload`,
method:'POST',
body: formData,
})
How do I implement this action in my .cds file.
It looks like this right now (simplified)
service ItemService {
type statusMessageType {
ok : Boolean;
message:String;
status: Integer;
ID:String;
};
action importEnvironment(fileId :String, envId :String, ver :String) returns statusMessageType;
action deleteItem(ID:String) returns statusMessageType;
action addFolder(folderName :String, currentFolderID :String) returns statusMessageType;
action renameItem(sourceID :String, targetID :String) returns statusMessageType;
action upload() returns statusMessageType;
}
How do I 'catch' the `formData` (body) in this `upload()` action as a parameter? And what type should it have? Binary?
Is this possible?
As a workaround I have implemented it as a normal express endpoint and it works in development.
But when I deploy my app to cloud foundry all my express implemented endpoints such as the following are not found and I get a 404 for them.
app.post('/upload');
app.get('/getItems');
Is this expected since we are not 'supposed' to do it like this?
Taking your example as a reference, the upload action would need to have a parameter defined as type Binary or LargeBinary. Then, you would need a custom event handler for the action (https://cap.cloud.sap/docs/guides/providing-services#custom-event-handlers). The file would be accessible to you through the req argument of the handler, as one of its parameters (https://cap.cloud.sap/docs/node.js/events#params).
So a hypothetical custom event handler for your action "upload" with argument "file" of type LargeBinary would look like this:
this.on ('upload', (req)=>{
const file = req.params.file;
//file upload to s3
})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@danilo_apicella1 I do have the custom handler written in my code. But it is not triggered on the post call, I get the error before that itself.
I'm using CAP Java.
User | Count |
---|---|
72 | |
9 | |
9 | |
8 | |
8 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.