How to download a file from Google Drive using paperclip-googledrive?

I uploaded the file to Google Drive, but I got an error during the download:

ActionController::MissingFile: Cannot read file original_22_Wages372-817339(wages).pdf 

I think he had no way to google.

Attachment Model:

 has_attached_file :doc, storage: :google_drive, google_drive_credentials: "#{Rails.root}/config/google_drive.yml", google_drive_options: { public_folder_id: '0BwCp_aK6VhiaQmh5TG9Qbm0zcDQ', path: proc { |style| "#{style}_#{id}_#{doc.original_filename}" } } 

Controller:

  attachment = Attachment.find(params[:id]) send_file attachment.doc.path(:original), filename: attachment.doc_file_name, type: attachment.doc_content_type, stream: 'true', x_sendfile: true 

Thanks in advance

+5
source share
2 answers

Dinesh, try the code below, it will solve your problem.

  attachment = Attachment.find(params[:id]) data = attachment.doc.url(:original) data = open(attachment.doc.url(:original)) send_file data, filename: attachment.doc_file_name, type: attachment.doc_content_type 
+1
source

You must first read the file from your application. And then make it available for download.

 attachment = Attachment.find(params[:id]) data = open(attachment.doc.url).read send_file data, :filename => attachment.doc_file_name, type: attachment.doc_content_type, stream: 'true', :x_sendfile => true 

You can also make it available, for example:

 redirect_to attachment.doc.url 

But this is the wrong way to do it. Because you directly open your resources to the end user.

+3
source

Source: https://habr.com/ru/post/1204839/


All Articles