- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
代码:
image_upload.js
function uploadAttachment(attachment) {
var file = attachment.file;
var form = new FormData;
form.append("Content-Type", file.type);
form.append("forum_post_photo[image]", file);
var xhr = new XMLHttpRequest;
xhr.open("POST", "/forum_post_photos.json", true);
xhr.setRequestHeader("X-CSRF-Token", Rails.csrfToken());
xhr.upload.onprogress = function(event){
var progress = event.loaded / event.total * 100;
attachment.setUploadProgress(progress);
}
xhr.onload = function(){
if (xhr.status == 201){
var data = JSON.parse(xhr.responseText);
return attachment.setAttributes({
url: data.image_url,
href: data.image_url
})
}
}
return xhr.send(form);
}
document.addEventListener("trix-attachment-add", function(event){
var attachment = event.attachment;
if (attachment.file){
console.log('new',attachment);
return uploadAttachment(attachment);
}
});
shrine.rb
require "shrine/storage/s3"
s3_options = {
bucket: Rails.application.credentials.aws[:bucket_name], # required
access_key_id: Rails.application.credentials.aws[:access_key_id],
secret_access_key: Rails.application.credentials.aws[:secret_access_key],
region: Rails.application.credentials.aws[:bucket_region],
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "cache",upload_options: { acl: "public-read" } , **s3_options),
store: Shrine::Storage::S3.new(prefix: "store",upload_options: { acl: "public-read" } ,**s3_options),
}
Shrine.plugin :activerecord
Shrine.plugin :presign_endpoint, presign_options: -> (request) {
filename = request.params["filename"]
type = request.params["type"]
{
content_disposition: "inline; filename=\"#{filename}\"", # set download filename
content_type: type, # set content type (required if using DigitalOcean Spaces)
content_length_range: 0..(10*1024*1024), # limit upload size to 10 MB
}
}
Shrine.plugin :restore_cached_data
trix 上传
require "shrine/storage/s3"
s3_options = {
bucket: Rails.application.credentials.aws[:bucket_name], # required
access_key_id: Rails.application.credentials.aws[:access_key_id],
secret_access_key: Rails.application.credentials.aws[:secret_access_key],
region: Rails.application.credentials.aws[:bucket_region],
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "cache",upload_options: { acl: "public-read" } , **s3_options),
store: Shrine::Storage::S3.new(prefix: "store",upload_options: { acl: "public-read" } ,**s3_options),
}
Shrine.plugin :activerecord
Shrine.plugin :presign_endpoint, presign_options: -> (request) {
filename = request.params["filename"]
type = request.params["type"]
{
content_disposition: "inline; filename=\"#{filename}\"", # set download filename
content_type: type, # set content type (required if using DigitalOcean Spaces)
content_length_range: 0..(10*1024*1024), # limit upload size to 10 MB
}
}
Shrine.plugin :restore_cached_data
trix-upload
function uploadAttachment(attachment) {
var file = attachment.file;
var form = new FormData;
form.append("Content-Type", file.type);
form.append("forum_post_photo[image]", file);
var xhr = new XMLHttpRequest;
xhr.open("POST", "/forum_post_photos.json", true);
xhr.setRequestHeader("X-CSRF-Token", Rails.csrfToken());
xhr.upload.onprogress = function(event){
var progress = event.loaded / event.total * 100;
attachment.setUploadProgress(progress);
}
xhr.onload = function(){
if (xhr.status == 201){
var data = JSON.parse(xhr.responseText);
return attachment.setAttributes({
url: data.image_url,
href: data.image_url
})
}
}
return xhr.send(form);
}
document.addEventListener("trix-attachment-add", function(event){
var attachment = event.attachment;
if (attachment.file){
console.log('new',attachment);
return uploadAttachment(attachment);
}
});
长话短说,我在论坛上使用 trix 作为富文本,所有模型和 Controller 都在工作,我正尝试通过拖放到编辑器中直接上传,如图所示 here但无法正确获取 js。
所有其他配置都是直接从 documentation 设置的
照片正在上传到我的 aws,但几分钟后就会过期
即使我的存储桶设置为公开阅读
任何帮助都会很棒我迷路了!
最佳答案
今天也遇到了这个问题。
还将 Shrine 与 AWS S3 一起使用,并在旧的 Rails 应用程序中使用 Trix。
我注意到图像存在于 S3 服务器中,只是 Trix 使用的 URL 不起作用。
在搜索了一些其他类似的问题后,遇到了这个:https://stackoverflow.com/a/51197576/2561325
答案来自神社 gem 的维护者之一。您所要做的就是在 config/initializers/shrine.rb
中的 shrine 初始化程序中应用公共(public)设置。
我的问题现在已解决,Trix 编辑器使用的图像不会过期。
关于javascript - 尝试使用 aws rails 5.2 的 shrine direct_upload 实现拖放直接上传时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53025874/
我已经设置了使用 Shrine 直接上传到 S3。这很好用。其中,我启用了以下插件: Shrine.plugin :backgrounding Shrine.plugin :store_dimensi
我在 Ruby on Rails 应用程序中使用 Shrine 来创建调整大小和将图像上传到存储的过程。 我当前的代码是: image_uploader.rb require "image_proce
我已经苦苦挣扎了大约 5 个小时,试图理解为什么 Shrine 会阻止我的上传。我要么在强参数中收到诸如“神殿:无效文件”之类的错误,要么收到“预期数组但得到字符串”之类的错误。如果没有错误,则图像实
我无法使用 Shrine 播种图像,与 Carrierwave 不同,下面的代码不起作用。 Profile.create! id: 2, user_id: 2,
我还有一些可以对图像执行的其他任务。比如选择多张图片,然后将它们组合成一张图片。我有使用 RMagick 和本地文件的那部分,我有使用 Shrine 的上传部分,但我需要将两者连接起来。上传图像后(理
我还有一些可以对图像执行的其他任务。比如选择多张图片,然后将它们组合成一张图片。我有使用 RMagick 和本地文件的那部分,我有使用 Shrine 的上传部分,但我需要将两者连接起来。上传图像后(理
我正在尝试使用多态关联和 Shrine 实现多个文件上传。 class Campaign :signature add_metadata(:md5) { |io| calculate_signa
显然 :remove_attachment插件在检查和提交方面起到了作用,但如何从 Controller 调用该方法? 最佳答案 所有允许您设置表单字段的插件( remove_attachment 、
我正在使用 shrine用于通过 Ruby on Rails 在 S3 中存储图像的 gem。 我怎样才能让 shrine 表现如下? 1. When uploading files from Fro
我正在使用 shrine用于通过 Ruby on Rails 在 S3 中存储图像的 gem。 我怎样才能让 shrine 表现如下? 1. When uploading files from Fro
我使用带有 Shrine gem 的 Rails 5.2 来上传图片。在客户端,我使用 NativeScript 6.0 和 Angular 8.0。 我已经安装了 Shrine,它在 Rails 端
我设法使用 shrine 将文件上传到 s3,但我试图根据它所属的相册将每张照片上传到不同的文件夹。 假设我有一个名为:abc 的存储桶: 上传图片到相册:family应该上传图片到:abc/fami
Shrine 是否支持在 S3 存储桶内的文件夹之间复制/移动文件的方法? 例如,我将一个文件上传到一个名为 cache 的文件夹中,如果一切正常,我将该文件移动到一个 store 文件夹中并清除缓存
代码: image_upload.js function uploadAttachment(attachment) { var file = attachment.file; var form
以下代码使用 RSpec 3.5 和 Shrine gem 在 Rails 4.2 应用程序的模型规范内测试图像验证。用于文件上传。 我的问题是: 您能想出改进以下测试的方法或更好的方法来测试这些验证
我在尝试允许使用 Shrine 上传头像时遇到了一些问题。我正在使用 Rails 5。我不断收到错误消息,“nil:NilClass 的未定义方法‘cached_image_data’”。 我试过多次
我是一名优秀的程序员,十分优秀!