How to download files through a custom action in Rails Admin - ruby-on-rails

How can I download a file through a custom action in Rails Admin. As the file which I'm sending is ZIP format but after getting downloaded its a nested zip which on opening creates another zip file and just goes on.
So basically this is my custom action where I'm calling my Service which will create a zip file through gem rubyzip and I'm sending the file_path of the created zip file with is on my local machine(e.g file_path as "#{Rails.root}/abc.zip")
else
# all your code that does the work
import_params = params.require(:documents)
.permit(:email)
if import_params[:email].present?
#result = Download::Forms.call(mail_id: import_params[:email])
if #result[:status]
flash.now[:notice] = 'File Downloaded'
send_file(
#result[:file_path],
filename: #result[:file_name],
type: "application/zip"
)
else
flash.now[:error] = "Failed to Download because #{#result[:error_message]}"
end
else
flash.now[:notice] = 'Please Enter Email Address'
end
#result
end
As I also send the #result in the end so that I can also try link_to method to download the file but all in vain. I'm using Rails 4.2.6
Some more information regarding the file types is that I tried a txt file and a pdf file also to check whether they are downloading properly but the same result they got downloaded but they were not proper as txt file was containing nothing and pdf can't be opened.
The files are getting downloaded with my custom action name and not from the file name which is the right naming if I download a file from any simple action.
Any help will be really appreciated.

Related

When I download Excel files, strange files are stored

I want to download uploaded files such as .zip, .xlsm, xlsx, pdf... ..., etc., that have been uploaded, I want to eventually compress them into a zip file for download.
However, if the uploaded files are not zipped, they will be downloaded with strange files stored in them.
In this case, .xlsm
Source Code
class DownloadZipsController < ::ApplicationController
def index
file_name = "#{Time.current.strftime("%Y%m%d_%H%M%S")}.zip"
zip_file = Tempfile.new(file_name)
Zip.unicode_names = true
Zip::OutputStream.open(zip_file.path) do |zip|
params[:product_ids].each do |product_id|
product = Product.find(product_id)
zip.put_next_entry("output.zip")
zip.print Net::HTTP.get URI.parse(product.submit_zip_file.url)
end
end
send_file zip_file.path,
type: "application/zip",
disposition: "attachment",
filename: file_name
zip_file.close
rescue => e
logger.debug e.backtrace
redirect_to request.referer, alert: e.message
end
end
Uploaded files are stored in AWS S3.
Is there any solution to this problem?
Office Documents such as .xlsm, .xlsx, .docx, and others are in fact zip files containing the document as a xml file plus additional resources.
The file tree you have shown in your screenshot shows the content of one such document if you interpret it as a zip file and unpack.
It appears that somewhere in your code, you have detected the document file as a zip file and interpreted it as such which resulted in its contents to be unpacked.
This is not apparent from the code you have posted though, so I would assume that you have some additional handling of zip files somewhere else (such as a function to download existing files which may then be send with a wrong content type to the browser, i.e as an application/zip rather than application/vnd.ms-excel.sheet.macroEnabled.12).

How to send the zip file in Rails via Grape API

I have a set of files that are present in s3 and I have to zip them all and send the zipped file to the front end(ReactJS).
I am successfully able to create a folder in the tmp of the project and also zip them. Unfortunately, I get the error when I try to expand saying Unable to expand
Here is the code -
data = Zip::File.open(zip_file_name, ::Zip::File::CREATE) do |zipfile|
files.each do |file|
zipfile.add(file, file_path)
end
end
content_type "application/octet-stream"
header['Content-Disposition'] = "attachment; filename=abcd.zip"
env['api.format'] = :binary
File.open(zip_file_name, 'rb').read
Is there a way to solve the problem? Thanks

Rails: Rename a Binary File uploaded to API

I've seen a lot of threads that open up a file from a directory and rename it. But in my case, the user is uploading a file to a POST endpoint. I'd like to rename that file before uploading to my blob storage.
Here's what I have so far:
def picture
#file = params[:file]
#new_file_name = generate__filename()
#this line didn't work
File.rename(#file, #new_file_name + File.extname(#file))
# begin upload here
end
I'm testing this by selecting the form-data value as file in Postman. How do I rename this file?

Email attachment for document files using rails action mailer

I am using Rails action mailer. I need to send a email with document(doc, docx, txt) as attachment. When I am attaching .txt files, the content in that txt file are viewing properly. But when I am trying to attach .doc or docx files, the content in that files are not displaying(it is converting into encoding format) Why is that happening. Is there any way to attach document files in email using rails action mailer.
def send_email(email, file)
#email = email
attachments['document.docx'] = File.read(file.path)
mail(from: #email, to: "aa#gmail.com", subject: "Document attachment")
end
original document attached
document received in email
Please guide me, if I am going wrong in some place.
Thanks in advance.
Try reading the file in binary mode (see binary file mode & :mode option:
attachments['document.docx'] = File.read(file.path, mode: 'rb')
In my case, Rails was adding a carriage return (aka "CR", or "\r") to every line in a CSV file that ended with either a "\n" (newline) or "\r".

Send .zip file as an Email Attachment in Rails

So I am trying to send an email containing a .zip file. The .zip file is located at another url on another server. I am able to retrieve the file, attach it and send it. However when I get the attachment from the email. It will not open as it says cannot open *.zip.zip I have tried removing the .zip in the name but then the archive manager cannot open it either.
Any ideas?
Code is below.
http = Net::HTTP.new('www.somedomaim.org')
http.start() { |http|
req = Net::HTTP::Get.new("/path/to/file.zip")
response = http.request(req)
tempfile = Tempfile.new('files')
File.open(tempfile.path, 'w') do |f|
f.write response.body
end
attachments["files.zip"] = File.read(tempfile.path)
mail to: someone#somewhere.com, subject: "Sending zip file"
}
[SOLVED]
The solution is rather simple.
attachments['files.zip'] = open('http://somedomain.com/path/to/file.zip').read
attachments needs to receive the content of the file. .read returns the content of the file. My issue was that i was placing the entire zip file in the content of a new file. The above solution will place just the content of the zip into a new file.
Hope this helps someone someday. Thanks for all the suggestions.
The solution is rather simple.
attachments['files.zip'] = open('http://somedomain.com/path/to/file.zip').read
attachments needs to receive the content of the file. .read returns the content of the file. My issue was that i was placing the entire zip file in the content of a new file. The above solution will place just the content of the zip into a new file.

Resources