How to upload a file using custom script?


#1

I have tried everything to get the file, upload using custom script but in vain. How can I accomplish this, the way we do using angular form data?

This is my code

var file =  event.record[0];
var options = {
    "CURLOPT_POSTFIELDS": '-----------------------------330603924205692451810523103\r\nContent-Disposition: form-data; name="files"; filename="test.txt\r\nContent-Type: text/plain\r\n'+file+'\r\n-----------------------------330603924205692451810523103--',
    "CURLOPT_HTTPHEADER": ["Content-type: multipart/form-data; boundary=---------------------------330603924205692451810523103"]
};
return platform.api.post("S3/bucket_name/container/folder/test.txt", null, options);

Access multipart/form-data information
#2

Hey Abhijit, have you checked out the File Storage Services documentation? Specifically, the section on creating files has an example, written in Angular, for uploading files to your file storage service.

https://github.com/dreamfactorysoftware/dsp-core/wiki/File-Storage-Services#creating-files


#3

I can do it using angular and dreamfactory but I am trying to achieve the same using server side scripting for some reason. But it seems it doesn’t work that way. Is there a solution for server side scripting?


#4

What is the format of your POST script? Generically, making API calls within scripting will take the form

platform.api.post( "service[/resource[/resource_id]]"[, payload] );

API Access via Scripts


#5

I’ve been trying to do something similar, but it seems like custom scripts don’t have a event.request.files object. Is that true?


#6

How is the file uploaded? Can you post an example of the REST call that POSTs the file, plus the part of your script that handles it and POSTs the payload to its destination?


#7

@jkhong, ya its true that event.request.files object doesn’t work in custom script. In fact i get nothing in event.request object on file upload.

@jeffreystables, below is the code

CLIENT CODE

var fd = new FormData();
fd.append('files', $scope.myFile);
$http.post("http://my_ip/rest/system/script/testFile?app_name=APP_NAME&is_user_script=true", fd, {
     transformRequest: angular.identity,
     headers: {'Content-Type': undefined}
});

#8

How are you handling the uploaded file in your custom script? You can just post the part of your script that handles it and POSTs the payload to its destination.