1
I’m making an XML reader to which I upload a file and it reads the content, in Chrome everything is working ok, but in firefox for some reason the drag and drop is not entering in $('#xmlenvio').change(...
, I’m not looking for them to do for me, I just need to understand what is happening, because it does not generate any error in the console.
Firefox version: 61.0.2 (64-bit) Chrome version: 69.0.3497.92 (64-bit)
function dropHandler(ev) {
ev.preventDefault();
var file;
if (ev.dataTransfer.items) {
for (var i = 0; i < ev.dataTransfer.items.length; i++) {
if (ev.dataTransfer.items[i].kind === 'file') {
document.querySelector('#xmlEnvio').files = ev.dataTransfer.files;
}
}
}
removeDragData(ev)
}
function dragOverHandler(ev) {
ev.preventDefault();
}
function removeDragData(ev) {
if (ev.dataTransfer.items) {
ev.dataTransfer.items.clear();
} else {
ev.dataTransfer.clearData();
}
}
$('#xmlEnvio').change(function(e){
alert('chrome works');
var input = this;
file = input.files[0];
$('.form-control.image-preview-filename').val(file.name);
var reader = new FileReader();
reader.onload = function() {
var parsed = new DOMParser().parseFromString(this.result, "text/xml");
console.log(parsed);
}
reader.readAsText(file);
console.log('... file[' + 0 + '].name = ' + file.name);
});
.upload-drop-zone {
height: 200px;
border-width: 2px;
margin-bottom: 20px;
}
.upload-drop-zone {
color: #ccc;
border-style: dashed;
border-color: #ccc;
line-height: 200px;
text-align: center
}
.upload-drop-zone.drop {
color: #222;
border-color: #222;
}
.image-preview-input {
position: relative;
overflow: hidden;
margin: 0px;
color: #333;
background-color: #fff;
border-color: #ccc;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container"> <br />
<div class="col-sm-12 text-center"></div>
<div class="col-sm-12">
<div class="panel panel-default">
<form id="upload" action="#" method="POST" enctype="multipart/form-data" accept-charset="UTF-8">
<div class="panel-heading">
<strong>Select XML archive</strong>
</div>
<div class="panel-body">
<div class="input-group image-preview">
<input placeholder="" type="text" class="form-control image-preview-filename" disabled>
<span class="input-group-btn">
<div class="btn btn-default image-preview-input">
<span class="glyphicon glyphicon-folder-open"></span>
<span class="image-preview-input-title">Search</span>
<input id="xmlEnvio" type="file" accept=".xml" name="xml"/>
</div>
<button type="submit" class="btn btn-labeled btn-primary">
<span class="btn-label">
<i class="glyphicon glyphicon-upload"></i>
</span>
Upload
</button>
</span> </div>
<br />
<div ondrop="dropHandler(event)" ondragover="dragOverHandler(event);" class="upload-drop-zone" id="drop-zone">
Or drag XML to this area.
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
I need it to stay on the onchange for the sake of reuse, as I will use the function both for the drag and drop issue and for when the client selects the file
– Alvaro Alves
As for shooting 2x in Chrome I had already found a "solution" in this way, but it is not satisfactory.
– Alvaro Alves
Easy, create a function called "parse" with Filereader reading, and call it in both dropHandler and onChange input
– Thalles Maia