Backendless Support
 
Solved

File uploading, and not returning fileURL

I am currently trying out the code present in the docs of the file APIs. The file always seems to upload properly, but never returns the file URL. This is the code I'm using.

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title>Files</title>
  6. <script src="https://api.backendless.com/sdk/js/v4/beta/backendless.min.js"></script>;
  7. </head>
  8. <body>
  9. <input type="file" id="files" name="files[]" multiple />
  10. <input type="button" onclick="uploadFileFunc(); return false;" value="Upload File"/>
  11. <script>
  12. Backendless.initApp( "XXXXXXXXXXXXXXXX", "XXXXXXXXXXXXX", "v1" );
  13. document.getElementById('files').addEventListener('change', handleFileSelect, false);
  14. function handleFileSelect(evt)
  15. {
  16. file = evt.target.files[0]; // FileList object
  17. }
  18. function uploadFileFunc()
  19. {
  20. var callback = {};
  21. callback.success = function(result)
  22. {
  23. }
  24. callback.fault = function(result)
  25. {
  26. alert( "error - " + result.message );
  27. }
  28. Backendless.Files.upload( file, "my-folder" )
  29. .then( function( fileURLs ) {
  30. console.log( "File successfully uploaded. Path to download: " + result.fileURL );
  31. })
  32. .catch( function( error ) {
  33. console.log( "error - " + error.message );
  34. })
  35. };
  36. </script>
  37. </body>
  38. </html>

Under "Backendless.Files.upload" the .then function does not return URLs. Is the code wrong?

Leave a Comment

Comments (3)

photo
1

I see two issues here:

  1. The "initApp" function in your example uses the 3.x format (see the 3rd argument in the call), however, you're importing the version 4 of the library.
  2. The .then callback in your code references the "result" variable, however, the promise function for the upload is declared with the "fileURLs" variable. I realize you copied from our example, which is not correct - we will fix it asap. The proper way to write that code is:

  1. .then( function( fileURL ) {
  2. console.log( "File successfully uploaded. Path to download: " + fileURL );
  3. })

photo
1

fileURL is returning [object Object]. Where can I get the actual URL?

Also, what is the result variable used for?

photo
1

That's right, it is an object. It contains a property called "fileURL", which is the actual URL. So to get it, you do the following (I renamed the response object to make it easier to understand):

  1. .then( function( response ) {
  2. var actualURL = response.fileURL;
  3. })

photo