gpt4 book ai didi

javascript - Rails : Uploading dropzone, S3,carrierwave,不适用于 Safari,但适用于 Google Chrome

转载 作者:可可西里 更新时间:2023-11-01 01:53:35 25 4
gpt4 key购买 nike

我将 dropzone 与 S3 和载波一起使用。我可以通过 Google Chrome 上传图片,但我无法让它与 Safari 一起使用,这很奇怪。

这是我的表格

= nested_form_for @trip, html: { multipart: true, id: 'fileupload', class: 'directUpload', data: { 'form-data' => (@s3_direct_post.fields), 'url' => @s3_direct_post.url, 'host' => URI.parse(@s3_direct_post.url).host } } do |f|
.dropzone#imageUpload
= f.simple_fields_for :trip_images, TripImage.new, child_index: TripImage.new.object_id do |ff|
= ff.file_field :photo, class: 'hide form-fields'
= f.button :submit, id: "submit-data"

这是在 Trip Controller 中

def set_s3_direct_post
@s3_direct_post = S3_BUCKET.presigned_post(key: "/uploads/temporary/#{SecureRandom.uuid}/${filename}", success_action_status: '201', acl: 'public-read', content_type: 'image/jpeg')
end

这是 TripImage 模型

class TripImage < ActiveRecord::Base
belongs_to :resource, :polymorphic => true
mount_uploader :photo, PhotoUploader
after_create :process_async

def to_jq_upload
{
'name' => read_attribute(:attachment_file_name),
'size' => read_attribute(:attachment_file_size),
'url' => attachment.url(:original),
'thumbnail_url' => attachment.url(:thumb),
'delete_url' => "/photos/#{id}",
'delete_type' => 'DELETE'
}
end

private

def process_async
PhotoVersioningJob.set( wait: 5.seconds ).perform_later(self.id)
end

end

这是js

$(function(){
$('.directUpload').find(".dropzone").each(function(i, elem) {
s3ImageUpload(elem);
});
})

function s3ImageUpload(elem){
var fileInput = $(elem);
var form = $(fileInput.parents('form:first'));
var form_url = form.data('url');
var form_data = form.data('form-data');
Dropzone.options.imageUpload = {
url: form_url,
params: form_data,
uploadMultiple: false,
addRemoveLinks: true,
removedfile: function(file){
//some codes
},
success: function(file, serverResponse, event){
//some codes
},
error: function(data){
//some codes
}
};
}

编辑:当前 CORS 配置

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*.example.com</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
<AllowedHeader>origin</AllowedHeader>
</CORSRule>
</CORSConfiguration>

已测试但不起作用

编辑:我也有 S3 直接上传,不确定这是否也会影响?

S3DirectUpload.config do |c|
c.access_key_id = Rails::AWS.config['access_key_id'] # your access key id
c.secret_access_key = Rails::AWS.config['secret_access_key'] # your secret access key
c.bucket = Rails::AWS.config['bucket_name'] # your bucket name
c.region = 's3' # region prefix of your bucket url. This is _required_ for the non-default AWS region, eg. "s3-eu-west-1"
end

最佳答案

我最近在 Safari 上遇到了类似的问题,发现它发送了一个额外的 Access-Control-Request-Header 而 Chrome 没有——特别是“来源”。为了解决这个差异,我需要更新目标存储桶上的 AWS CORS 配置。

AWS Documentation on the necessity of request headers matching an allowed header config .第三个要点明确了此要求:

Every header listed in the request's Access-Control-Request-Headers header on the preflight request must match an AllowedHeader element.

This helpful StackOverflow answer给出一个示例配置:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
<AllowedHeader>x-requested-with</AllowedHeader>
</CORSRule>
</CORSConfiguration>

为了让它在 Safari 中正常工作需要添加什么:

    <AllowedHeader>origin</AllowedHeader>

关于javascript - Rails : Uploading dropzone, S3,carrierwave,不适用于 Safari,但适用于 Google Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34216125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com