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).
Related
I'm trying to allow for a user to upload a zipped folder containing xsl stylesheets.
I then want to unzip the folder and save the folder in Rails.root/public/xsl/folderName
Here's what I'm trying in my action:
require 'zip'
Zip::File.open(params[:stylesheet].tempfile) do |zipFile|
zipFile.each do |file|
path = File.join(Rails.root.join('public','xsl'),file.name)
File.open(path, 'wb') do |f|
f.write(file)
end
end
end
I'm getting Zip::Error: Zip end of central directory signature not found from /GEMS/gems/rubyzip-1.3.0/lib/central_directory.rb:143:in `get_e_o_c_d`
This error is happening on the first line of the code. I've tried zipping the folder through 7Zip and sending the folder to Window's "Compressed (zipped) folder".
Thanks!
Zip::Error: Zip end of central directory signature not found from /GEMS/gems/rubyzip-1.3.0/lib/central_directory.rb:143:in get_e_o_c_d
Error indicates: Perhaps, the .xlsx file was corrupted also You can discover that the zip is corrupted or not by trying to unzip it from your code.
In solutiions: The content by forced it to encode as utf-8, and handle the buffer file after the upload also extract its data after it. I suggest to use the code of
roo-xls gem, and it's handle both .csv/.xlsx files very easy way.
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
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?
I'm using ruby on rails to modify a powerpoint presentations existing template's xml files (open xml) based on data from my postgres database.
The issue I am facing is that after the file is generated and downloaded from heroku using a windows machine, microsoft powerpoint detects the file as corrupted and attempts to repair. After repairing the file generated in powerpoint, the file opens correctly.
If i download the file from a Linux machine and send the file to a windows machine, the file opens correctly without warning or attempt to repair. The powerpoint generated also opens correctly on Open office.
Technically, these are the steps that I am using to generate the file.
Open the template from the assets folder of my application
Extract the files in a tmp folder using Rubyzip gem
Open and modify the individual files using Nokogiri
Compress the file to a .pptx file
Upload/Save file using active storage
Redirect user to the file for download
Extract files method
def self.extract_files(dir_prefix)
Zip::File.open(Rails.root.join('app', "assets", "ppt", 'test_8.pptx')) do |z|
z.each do |f|
##Extract files in a directory
f_path=File.join("tmp/#{dir_prefix}_destination", f.name)
FileUtils.mkdir_p(File.dirname(f_path))
z.extract(f, f_path) unless File.exist?(f_path)
end
end
end
Opening files for manipulations using:
chartxml = File.open(Rails.root.join('tmp', tmp_extract_folder, 'ppt', 'charts', 'chart5.xml'))
##Manipulation logic here
File.write(Rails.root.join('tmp', tmp_extract_folder, 'ppt', 'charts', 'chart5.xml'), doc.to_xml)
Re-zipping files using:
zf =ZipFileGenerator.new("tmp/#{dir_prefix}_destination", "tmp/#{dir_prefix}_zipped.pptx")
The implementation of the zip file generator is the same as the one provided on Rubyzip's github repo
I'm then storing using active storage using:
ppt = Powerpoint.new(name: "#{dir_prefix}")
ppt.file.attach(
io: File.open("tmp/#{dir_prefix}_zipped.pptx"), filename: 'Synthese.pptx', content_type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
)
ppt.save
render :js => "window.location = '#{download_ppt_path(dir_prefix: dir_prefix)}'"
The user then downloads the file:
ppt = Powerpoint.where(name: dir_prefix)
redirect_to rails_blob_path(ppt.first.file, disposition: "attachment")
I tried uncommenting the code which does the manipulation of the xml files, and just unzipping and rezipping; I still face the same issue.
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.