From 3ad002bdf1b2bde993a584b648f217b05d62329c Mon Sep 17 00:00:00 2001 From: Bohung Date: Thu, 23 Sep 2021 11:35:44 +0800 Subject: [PATCH] Fix drag upload bug. --- .../admin/archive_files_controller.rb | 11 +++++++++++ app/views/admin/archive_files/_form.html.erb | 16 ++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/archive_files_controller.rb b/app/controllers/admin/archive_files_controller.rb index 86b9ed0..986fb70 100644 --- a/app/controllers/admin/archive_files_controller.rb +++ b/app/controllers/admin/archive_files_controller.rb @@ -82,6 +82,17 @@ class Admin::ArchiveFilesController < OrbitAdminController render :partial => "categories_order" if request.xhr? end def create + params["archive_file"]["archive_file_multiples_attributes"].each do |k,v| + if v["file"].class == String + file_content_info = JSON.parse(v["file"]) + content_type = file_content_info["type"] + filename = file_content_info["name"] + head = "Content-Disposition: form-data; name=\"archive_file[archive_file_multiples_attributes][#{k}][file]\"; filename=\"#{filename}\" Content-Type: #{content_type}" + tempfile = Tempfile.new(filename,binmode: true) + tempfile.write(file_content_info["content"].unpack('U*').map{|i| i.chr }.join) + v["file"] = ActionDispatch::Http::UploadedFile.new({:filename=>filename,:type=>content_type,:head=>head,:tempfile=>tempfile}) + end + end rescue nil @archive_file = ArchiveFile.new(archive_vars) @archive_file.create_user_id = current_user.id @archive_file.update_user_id = current_user.id diff --git a/app/views/admin/archive_files/_form.html.erb b/app/views/admin/archive_files/_form.html.erb index 522684a..99e7fcd 100644 --- a/app/views/admin/archive_files/_form.html.erb +++ b/app/views/admin/archive_files/_form.html.erb @@ -249,13 +249,17 @@ } function FileListItems (files) { var b; - if(window.dataTransfer){ - b = window.dataTransfer; - }else{ - if(typeof(ClipboardEvent) == "undefined"){ //IE - b = new DataTransfer(); + try{ + b = new DataTransfer(); + }catch(e){ + if(window.dataTransfer){ + b = window.dataTransfer; }else{ - b = new ClipboardEvent("").clipboardData; + if(typeof(ClipboardEvent) == "undefined"){ //IE + b = new DataTransfer(); + }else{ + b = new ClipboardEvent("").clipboardData; + } } } if(b.items){