We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 9 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
在您的控制器中:
在
config/environment/Production.rb
中:或
需要修改配置,因为它使 Web 服务器能够直接从磁盘发送文件,这会带来很好的性能提升。
更新
如果您想显示它而不是下载它,请使用
send_file
的:disposition
选项:如果您想内联显示它,< a href="https://stackoverflow.com/questions/291813/best-way-to-embed-pdf-in-html">这个问题将比我更完整。
In your controller:
In
config/environment/production.rb
:or
The config modification is required because it enables the web server to send the file directly from the disk, which gives a nice performance boost.
Update
If you want to display it instead of downloading it, use the
:disposition
option ofsend_file
:If you want to display it inline, this question will be much more complete that I could ever be.
基本上你只需要把它写在你的视图中的html中。所以这个简单的解决方案对我有用:
在“show.hmtl.erb”中,
只需将文件位置放入嵌入式 ruby 中作为 iframe 标记的源,经过数小时的搜索后,对我来说很有效。 “certificate”是我的模型,“certificate_pdf”是我的附件文件。
Basically you just need to write it in the html in your view. So this simple solution worked for me:
In the 'show.hmtl.erb'
just putting the file location in embedded ruby as the source of the iframe tag worked for me after hours and hours of searching. 'certificate' is my model, and 'certificate_pdf' is my attachment file.
根据 PDF 的来源,以下内容可能会对您有所帮助。我有一个应用程序,其中存储了很多东西,其中一些有(附加)PDF 文件连接到这些项目。我将项目存储在目录
/public/res//
中。res
表示结果,item_id
是 Rails 中该项目的数字 ID。在视图中,我通过以下(伪)代码作为辅助方法提供了 PDF 的链接,该方法可以在视图中使用:
此处的相关部分是
link_to 名称,“/res/#{ res.id}/#{File.basename(file)}"
东西。Depending where the PDF comes from, the following may help you. I have an application where I store a lot of things, and some of them have (additional) PDFs connected to the items. I store the items in the directory
/public/res/<item_id>/
.res
means result, anditem_id
is the numeric id of that item in Rails.In the view, I provide a link to the PDFs by the following (pseudo-)code as a helper method, that may be used in the view:
The relevant part here is the
link_to name, "/res/#{res.id}/#{File.basename(file)}"
thing.这可能太简单了,但我很难找到问题的简单答案,所以我将其发布在这里。我真的不想只是为了下载静态文件而向控制器添加另一个操作。
我刚刚将我的文件上传到 S3 &使用 link_to 来指代路径,这是 S3 在您上传文件并设置权限后提供的路径(记住,每个人都必须能够上传和下载它)。我在 S3 上存储了应用程序的大量数据,因此这似乎是一个不错的选择。
This may be too simple, but I had trouble finding a simple answer to my problem, so I'm posting it here. I really didn't want to add another action to my controller just to download a static file.
I just uploaded my file to S3 & used link_to referring to the path, which S3 provides after you upload the file and set the permissions (remember, everyone has to be able to upload and download it). I store lots of data for the app on S3 so it seemed like a fine choice.