Open a paperclip attached word doc in Ruby/rails

2019-09-19 19:37发布

问题:

I have a rails app with file attachments using paperclip. Images and pdfs work fine in that if I click on the linked attachment, the image or pdf opens. If I right click, I can do a save-as.

However, with Office files such as word.docx, I can only download the file. If I go do Dos and type the filename, it will open in MS word. Therefore, I think that I should be able to use the system or system call and just pass the attachment name from paperclip (ie attachment.image.url) to dos and the file should open. I think the command I want would be something like

system(attachment.image.url)

I can't figure out how to write the statement to do that.

回答1:

Your browser will not (and should not) allow you to execute anything outside of the browser itself, this means that without some external app making the call to your Rails app from the clients machine, you will not be able to open the Word doc without downloading it.

You are able to open the images and PDFs in the browser because the browser supports them, but most browsers won't open Word docs.

You can either have the user download the file, or maybe use a third party viewer that you can open the word doc with inside the browser.