How to upload file on dropbox without saving a copy to hosting server

How to upload file on dropbox without saving a copy to hosting server

shatrughanshatrughan Posts: 87Questions: 16Answers: 0
edited May 13 in Editor

Owing to space crunch of my web hosting server, I want to save my files on dropbox using the upload field of editor. My files are being saved to both hosting account as well as at dropbox. I'm unable to figure out how to customize the upload action to save at dropbox only.

    { 
         label: "Upload File:",
         name: "Image",
         type: "upload",
         display: function(file_id, row, type, data) {
             if (file_id) {
                 var local_file_path = childTable_Editor.file('files_Certificates', file_id).web_path;
    
                 var linkClass = 'link-' + file_id;
    
                 var initialLink = '<a class="' + linkClass + '" href="#" target="_blank">Loading link...</a>';
    
                 $.ajax({
                     url: 'dropbox_upload.php',
                     type: 'POST',
                     async: true,
                     data: {
                         'local_file_path': local_file_path,
                         'Party_Code': <?php echo "'".$_SESSION['Section']."'"; ?>,
                         'Division': <?php echo "'".$_SESSION['Division']."'"; ?>,
                         'Region': <?php echo "'".$_SESSION['Region']."'"; ?>,
                         'filename':childTable_Editor.field( 'Certificate_Number' ).get(),
                         'dropbox_path': "/FCI/"+<?php echo "'".$_SESSION['Region']."'"; ?>+"/"+<?php echo "'".$_SESSION['Division']."'"; ?>+"/"+<?php echo "'".$_SESSION['Section']."'"; ?>+"/FSSAI/"
                     },
                     success: function(response) {
                         var result = JSON.parse(response);
                         if (result.success) {
                             $('.' + linkClass).attr('href', result.link);
                             $('.' + linkClass).text('Uploaded file id : ' + file_id);
                         } else {
                             alert(result.message);
                         }
                     },
                     error: function(xhr, status, error) {
                         alert("An error occurred: " + error);
                     }
                 });
    
                 return initialLink;
             } else {
                 return 'No file';
             }
         },
        }

My php script as follows -

    Field::inst( 'Image' )
        ->setFormatter( Format::ifEmpty( null ) )
        ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/Certificates/__NAME__.__EXTN__' )
        ->db( 'files_Certificates', 'id', array(
            'filename'    => Upload::DB_FILE_NAME,
            'filesize'    => Upload::DB_FILE_SIZE,
            'web_path'    => Upload::DB_WEB_PATH,
            'system_path' => Upload::DB_SYSTEM_PATH
        ) )->validator( Validate::fileExtensions( array( 'png', 'jpg','pdf', 'jpeg', 'gif','xls','xlsx' ), "Please upload proper file i.e. image or pdf" ) )
        )

Kindly suggest me the necessary changes to resolve the ibid issue.

Thanks & Regards
Shatrughan

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 62,858Questions: 1Answers: 10,344 Site admin
    Answer ✓

    Hi Shatrughan,

    Do Dropbox provide an uploading API? They probably do, but I don't know what it is. Assuming they do, the way to do it would be to use a custom upload action. With that, the file would be uploaded to your server, and then use the API to copy it to Dropbox via their API, and finally delete the temporary file on the server.

    If you don't want the file to touch your server at all, you can use the ajax option of the upload type to handle the file upload, instead of the default Ajax upload, but you'd need to find a Dropbox Javascript client that can upload the file. Again, I don't know if that is even possible or not. It would certainly have security implications.

    Allan

  • shatrughanshatrughan Posts: 87Questions: 16Answers: 0

    The first option using temporary directory to save the file at server and deleting it after being stored at Dropbox seems to be a better option. Another option which may be useful for other forum users if needed to have a custom file control and upload the file directly to cloud storage with concerned API while saving url of the dropbox file in database.

    Thanks a lot, @allan

Sign In or Register to comment.