現在、それぞれのブラウザで動作チェックを行なっているのですが、safariだけjsが反応しないので質問させていただきました。safariで動作するためにはどうしたら良いのでしょうか?
view
1<div class="images-field clearfix"> 2 <%= f.label :image %> 3 <%= f.file_field :images, multiple: true, class: "file-input" %> 4 <% if @item.images.attached? %> 5 <% @item.images.each do |image| %> 6 <div class="image-box"> 7 <%= image_tag image.variant(resize: '100x100'), class:"item_images" %> 8 <p> <%= image.filename %> </p> 9 <%= f.hidden_field :images , name: "item[images][]", value: "#{image.blob.id}", style: "display: none;", class: "item-images-input" %> 10 <%= link_to "Edit", "", class: "btn-edit" %> 11 <%= file_field "edit-image","" , class: "edit-image-file-input file-input", style: "display: none;"%> 12 <%= link_to "Delete", "", class: "btn-delete" %> 13 </div> 14 <% end %> 15 <% end %> 16</div>
js
1$(document).on('click', function() { 2 3 $('#item_images').on('change',function(e){ 4 var files = e.target.files; 5 var d = (new $.Deferred()).resolve(); 6 $.each(files,function(i,file){ 7 d = d.then(function() { 8 return Uploader.upload(file)}) 9 .then(function(data){ 10 return previewImage(file, data.image_id); 11 }); 12 }); 13 $('#item_images').val(''); 14 }); 15 16 $('.images-field').on('click','.btn-edit', function(e){ 17 e.preventDefault(); 18 $(this).parent().find('.edit-image-file-input').trigger("click"); 19 }); 20 21 $('.images-field').on('change','.edit-image-file-input', function(e){ 22 var file = e.target.files[0]; 23 var image_box = $(this).parent(); 24 Uploader.upload(file).done(function(data){ 25 replaceImage(file, data.image_id, image_box); 26 }); 27 }); 28 29 $('.images-field').on('click','.btn-delete', function(e){ 30 e.preventDefault(); 31 $(this).parent().remove(); 32 }); 33 34 var replaceImage = function(imageFile, image_id, element){ 35 var reader = new FileReader(); 36 var img = element.find('img'); 37 var input = element.find('.item-images-input'); 38 var filename = element.find('p'); 39 reader.onload = function(e){ 40 input.attr({value: image_id}); 41 filename.html(imageFile.name); 42 img.attr("src", e.target.result); 43 }; 44 reader.readAsDataURL(imageFile); 45 } 46 47 var previewImage = function(imageFile, image_id){ 48 var reader = new FileReader(); 49 var img = new Image(); 50 var def =$.Deferred(); 51 reader.onload = function(e){ 52 var image_box = $('<div>',{class: 'image-box'}); 53 image_box.append(img); 54 image_box.append($('<p>').html(imageFile.name)); 55 image_box.append($('<input>').attr({ 56 name: "item[images][]", 57 value: image_id, 58 style: "display: none;", 59 type: "hidden", 60 class: "item-images-input"})); 61 image_box.append('<a href="" class= "btn-edit">Edit</a>'); 62 image_box.append($('<input>').attr({ 63 name: "edit-image[]", 64 style: "display: none;", 65 type: "file", 66 class: "edit-image-file-input file-input"})); 67 image_box.append('<a href="" class="btn-delete">Delete</a>'); 68 $('.images-field').append(image_box); 69 img.src = e.target.result; 70 def.resolve(); 71 }; 72 reader.readAsDataURL(imageFile); 73 return def.promise(); 74 } 75 76 var Uploader = { 77 upload: function(imageFile){ 78 var def =$.Deferred(); 79 var formData = new FormData(); 80 formData.append('image', imageFile); 81 $.ajax({ 82 type: "POST", 83 url: '/items/upload_image', 84 data: formData, 85 dataType: 'json', 86 processData: false, 87 contentType: false, 88 success: def.resolve 89 }) 90 return def.promise(); 91 } 92 } 93})