A file is being uploaded by rails and is logged as follows:
Parameters: {"authenticity_token"=>"[FILTERED]", "account"=>{"main_file"=>#<ActionDispatch::Http::UploadedFile:0x000000010922f7b8 #tempfile=#<Tempfile:/var/folders/jf/13j7d83s4_79kb5c4lkgdy0r0000gn/T/RackMultipart20221116-18676-44klzi.js>, #original_filename="smaller_sample_array.js"
The following does not work as the file is not a string per se
params[:account][:main_file] = params[:account][:main_file].reverse.chomp("revision.main.categories.part.0 = ".reverse).reverse
> NoMethodError (undefined method `reverse' for #<ActionDispatch::Http::UploadedFile:
while there is a method for renaming:
self.active_storage_object.blob.update(filename: "#{account_id}_#{self.active_storage_object.filename}.json")
what is a recommended method - or flow of methods - to alter (reverse chomp) the file contents?
Bote: if a file is first saved, then calling in this case account.file (Account has_one_attached :file), the view shows:
ActiveStorage::Attached::One:0x0000000106775338>
The file saved under the storage directory can be found, but have no idea how to access it from the rails controller.
Related
I wants to read a excel file existing on Live URL of another website.
When I hit that URL in browser file is downloading. While in my rails app it is giving below error
No such file or directory # rb_sysopen - http://www.carsa.jp/admin/data.xlsx (Errno::ENOENT)
My Rails app code is as below
data = Roo::Excelx.new('http://www.carsa.jp/admin/data.xlsx')
header = data.row(1)
puts header
Note: If I download file and place it within my application it is working fine but the requirement is to read it from the third-party website in a scheduled job as per the above script.
data = Roo::Excelx.new('lib/data.xlsx')
header = data.row(1)
puts header
Try using Roo::Spreadsheet.open instead of Roo::Excelx.new. According to the Roo Readme:
Roo::Spreadsheet.open can accept both paths and File instances.
This should do the trick:
Roo::Spreadsheet.open('http://www.carsa.jp/admin/data.xlsx')
I have an app that lets users download itineraries if they like the preview of the first page. In order to make the preview, I use PDF Reader to parse the original document and create a PDF version of the first page.
In development, the original itinerary is stored in the public folder.
#itin_preview = itinerary
reader = PDF::Reader.new("public/#{#itin_preview/document_file_name}")
formatted_page = reader.page(1)
formatted_text = formatted_page.text.gsub! /\s+/, ''
page_text = formatted_text.gsub!(/$/, "\n")
This return public/itinerary.pdf. However, in production, the itinerary is stored on Amazon AWS, so i tried to configure an initializer with different paths in a YAML file for development and production, taken from here.
config.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/initializers/config.yml")
config.yml
development:
itinerary_path: /public/#{#itin_preview.document_file_name}
production:
itinerary_path: <%= asset_path "#{#itin_preview.document_file_name}" %>
When I input the constant into my PDF Reader like so,
reader = PDF::Reader.new(APP_CONFIG[Rails.env]['itinerary_path'])
I get the following error:
Completed 500 Internal Server Error in 19ms (ActiveRecord: 1.3ms)
ArgumentError (input must be an IO-like object or a filename):
I have double checked that APP_CONFIG[Rails.env]['itinerary_path'] spits out public/itinerary.pdf in development. So why does it work as a string but not as a Constant?
How can i get it to work?
I am trying to download a zip file from an FTP server and save it in a temporary directory:
agent = Net::FTP.new
agent.connect(host, 21)
agent.login(user, pass)
agent.chdir(path)
agent.passive = true
file = agent.getbinaryfile(source_file, #file_path)
file.save!(#file_path)
However, it doesn't save the file because it's a String?
NoMethodError: undefined method `save!' for #<String:0x0000000cbc0398>
Is there another way to download zip files from an FTP server, or what am I missing?
Calling getbinaryfile is enough because Net::FTP#getbinaryfile will save the content in the specified path.
....
agent.getbinaryfile(source_file, #file_path)
The exception is thrown because the method returns a string and String does not have save! method. (Net::FTP does not have save! method neither)
I'm trying to attach a file to an email. The file is in assets/downloads/product.pdf
In the mailer, I have:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_path("product.pdf"))
I've tried:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_url("product.pdf"))
...and even:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.compute_asset_host("product.pdf") + ActionController::Base.helpers.compute_asset_path("product.pdf"))
I always get the same error:
EmailJob crashed!
Errno::ENOENT: No such file or directory - //localhost:3000/assets/product.pdf
...or a variation on the theme. But even when I try using asset_url in the view or just put the url in the browser it works:
http://localhost:3000/assets/product.pdf
I've also tried using straight up:
File.read("app/assets/downloads/product.pdf")
File.read("downloads/product.pdf")
...which works in dev environment but not on staging server (heroku). Error is still:
Errno::ENOENT: No such file or directory - downloads/product-market-fit-storyboard.pdf
Also tried:
File.read("/downloads/product.pdf")
File.read("http://lvh.me:3000/assets/product.pdf")
...don't work at all.
Ideas?
you should use syntex like this.it is work for me may be will work for you also.
File.open(Dir.glob("#{Rails.root}/app/assets/downloads/product.pdf"), "r")
When using mailer, you shouldn't use assets pipline. Asset pipeline would be useful if you wanted to have link to a file inside your email. When rendering an email, action mailer has access to files in app directory.
Please read about attachments in action mailer guide. As you can see, you just need to pass path to a file, not url:
attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
I am trying to save a simple template to pdf using the rendering plugin, but I cannot get it to work no matter what I try. All I need is for it to save a file within the file system on the server and redirect to a different page.
At the minute the pdf template does not need any parameters as it just prints hello world. Once I get this working I will attempt to add some data.
I am getting errors saying I need to specify a controller if no '/' is appended. But I have tried adding this to no avail. Plus I don't understand which controller it needs as I have tried specifying the controller this action is declared.
Can someone please have a look at this and tell me what I'm doing wrong?
RenderingService pdfRenderingService
def displayPDFSummary = {
ByteArrayOutputStream bytes = pdfRenderingService.render(template: "_pdfTemplate", controller:"RSSCustomerOrder", model: [origSessionId:params.origSessionId])
def fos= new FileOutputStream('NewTestFile.pdf')
fos.write(bytes)
fos.close()
render(template: "_pdfTemplate", params: [origSessionId:params.origSessionId])
}
I am getting the following error messages in the console:
groovy.lang.MissingMethodException: No signature of method: java.io.FileOutputStream.write() is applicable for argument types: (java.io.ByteArrayOutputStream)
(Then prints contents of template...)
Possible solutions: write([B), write(int), write([B), write(int), wait(), wait(long)
Did you look at the FileOutputStream docs? There's no write(OutputStream) method.
Try fos.write(bytes.toByteArray()). Also, bytes.writeTo(fos) may work.