Fix bug for IE drag upload file.

This commit is contained in:
BoHung Chiu 2021-06-16 11:52:20 +08:00
parent 423a2f83db
commit 75e9d38bea
2 changed files with 97 additions and 3 deletions

View File

@ -1,4 +1,5 @@
class Admin::ArchiveFilesController < OrbitAdminController class Admin::ArchiveFilesController < OrbitAdminController
require 'action_dispatch/http/upload'
def show def show
module_pages = Page.where(:module => 'archive').collect{|p| p.url} module_pages = Page.where(:module => 'archive').collect{|p| p.url}
if module_pages.length<1 if module_pages.length<1
@ -100,6 +101,17 @@ class Admin::ArchiveFilesController < OrbitAdminController
# PUT /archive_files/1 # PUT /archive_files/1
# PUT /archive_files/1.xml # PUT /archive_files/1.xml
def update def update
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
@archive_file = ArchiveFile.find(params[:id]) @archive_file = ArchiveFile.find(params[:id])
@archive_file.update_user_id = current_user.id @archive_file.update_user_id = current_user.id

View File

@ -221,6 +221,32 @@
<% content_for :page_specific_javascript do %> <% content_for :page_specific_javascript do %>
<script> <script>
if (!FileReader.prototype.readAsBinaryString) {
console.log('readAsBinaryString definition not found');
FileReader.prototype.readAsBinaryString = function (fileData) {
var binary = '';
var pk = this;
var reader = new FileReader();
reader.onload = function (e) {
var bytes = new Uint8Array(reader.result);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
var a = bytes[i];
var b = String.fromCharCode(a)
binary += b;
}
pk.content = binary;
$(pk).trigger('onload');
}
reader.readAsArrayBuffer(fileData);
}
}
function FileListItems (files) { function FileListItems (files) {
var b; var b;
if(window.dataTransfer){ if(window.dataTransfer){
@ -241,6 +267,9 @@
delete b.files[i]; delete b.files[i];
} }
} }
if(b.files.length != 0){
return files
}
} }
for (var i = 0, len = files.length; i<len; i++){ for (var i = 0, len = files.length; i<len; i++){
if(b.items){ if(b.items){
@ -256,14 +285,63 @@
if(fileupload.length > 0){ if(fileupload.length > 0){
fileupload.find(".fileupload-preview").text(files[0].name); fileupload.find(".fileupload-preview").text(files[0].name);
} }
console.log(files)
var files_list = new FileListItems(files) var files_list = new FileListItems(files)
$(file_field)[0].files = files_list; try{
$(file_field)[0].files = files_list;
}catch(e){console.log(e)}
if($(file_field)[0].files.length == 0){ //Change failed if($(file_field)[0].files.length == 0){ //Change failed
var file_field_values = []; var file_field_values = [];
for(var i = 0; i < files_list.length; i++){ var file_reader = new FileReader();
$(file_field)[0].files[i] = files_list[i]; $("[name=\""+$(file_field)[0].name+"\"][type=\"hidden\"]").remove();
var hidden_input = $("<input type=\"hidden\" name=\""+$(file_field)[0].name+"\">");
var hidden_input_values = [];
$(file_field).after(hidden_input);
var files_list_length = files_list.length;
for(var i = 0; i < files_list_length; i++){
console.log(files_list[i].name)
var file = files_list[i];
$(file_field)[0].files[i] = file;
file_reader.readAsBinaryString(files_list[i]);
file_reader.onload = (function(hidden_input,file,i,files_list_length) {
return function(e) {
var file_info = {};
file_info["name"] = file.name;
file_info["type"] = file.type;
if (file_reader.result)
file_reader.content = file_reader.result;
file_info["content"] = e ? e.target.result : file_reader.content;
if(Array.isArray(hidden_input_values)){
hidden_input_values.push(file_info);
}
if(i == files_list_length - 1){
if(hidden_input_values.length == 1){
hidden_input_values = hidden_input_values[0];
}
hidden_input.val(JSON.stringify(hidden_input_values));
}
};})(hidden_input,file,i,files_list_length);
file_field_values.push("C:\\fakepath\\" + files_list[i].name); file_field_values.push("C:\\fakepath\\" + files_list[i].name);
} }
Object.defineProperty($(file_field)[0].files, "length", {
// only returns odd die sides
get: function () {
var length = 0;
while(this[length]){
length++;
}
return length;
}
});
Object.defineProperty($(file_field)[0], "value", {
// only returns odd die sides
get: function () {
return (this.getAttribute('value') ? this.getAttribute('value') : "");
},
set: function(value) {
this.setAttribute('value',value);
}
});
$(file_field)[0].value = file_field_values.join(", "); $(file_field)[0].value = file_field_values.join(", ");
} }
} }
@ -274,6 +352,7 @@
ev.preventDefault(); ev.preventDefault();
} }
function dropHandler(ev) { function dropHandler(ev) {
window.ev = ev;
window.dataTransfer = ev.dataTransfer; window.dataTransfer = ev.dataTransfer;
// Prevent default behavior (Prevent file from being opened) // Prevent default behavior (Prevent file from being opened)
ev.preventDefault(); ev.preventDefault();
@ -332,6 +411,9 @@
$(document).on('click', '.delete_file', function(){ $(document).on('click', '.delete_file', function(){
$(this).parents('.input-prepend').remove(); $(this).parents('.input-prepend').remove();
}); });
$(document).on('click',"[type='file']",function(){
$("[name=\""+$(this).attr("name")+"\"][type=\"hiiden\"]").remove();
});
$(document).on('click', '.remove_existing_record', function(){ $(document).on('click', '.remove_existing_record', function(){
if(confirm("<%= I18n.t(:sure?)%>")){ if(confirm("<%= I18n.t(:sure?)%>")){
$(this).children('.should_destroy').attr('value', 1); $(this).children('.should_destroy').attr('value', 1);