-2
jsonファイルを解析してサーバー(Asp.netコア)にアップロードするJavaScriptコードのブロックがあります。コードはクロムで正常に動作しますが、クリックイベントのIEでは何もエラーが発生していない、何も起こりません。 HTMLIEとモバイルブラウザでJavaScriptコードが機能しない
<form method="post" enctype="multipart/form-data">
<div class="input-group image-preview">
<input placeholder="File Name" id="LicenseFileText" style="border-bottom: 1px solid #fff" type="text" class="form-control " disabled="disabled">
<span class="input-group-btn">
<span class="btn btn-default image-preview-input">
<span class="fa fa-folder-open"></span>
<span class="image-preview-input-title">Browse</span>
<input type="file" id="files" name="files" accept=".json"/>
</span>
<button type="button" id="Upload" class="btn btn-labeled btn-info upload"><i class="fa fa-arrow-circle-o-up"></i> Upload</button>
</span>
</div>
</form>
Javascriptを-コピー
document.querySelector('.upload').addEventListener('click', function (evt) {
if (evt.target.tagName.toLowerCase() == 'button') {
var startByte = evt.target.getAttribute('data-startbyte');
var endByte = evt.target.getAttribute('data-endbyte');
readBlob(startByte, endByte);
}
}, false);
$('#files').change(function() {
$('#LicenseFileText').val($(this).val().split('\\').pop());
});
//機能
function readBlob(opt_startByte, opt_stopByte) {
var files = document.getElementById('files').files;
if ($("#LicenseFileText").val() == "") {
NiceAlert.showNotificationError('top', 'center', 'Select a valid file');
return ;
}
var file = files[0];
var start = parseInt(opt_startByte) || 0;
var stop = parseInt(opt_stopByte) || file.size - 1;
var reader = new FileReader();
// If we use onloadend, we need to check the readyState.
reader.onloadend = function (evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
var licenseObj = JSON.parse(evt.target.result);
$.ajax({
url: "/GkAccounts/CreateLicense",
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(licenseObj),
success: function (message) {
LoadLicense();
CheckLicenseValidity();
NiceAlert.showNotificationSuccess('top', 'center', 'License Updated');
$("#LicenseFileText").val("");
},
error: function() {
NiceAlert.showNotificationError('top', 'center', 'Validation Failed');
$("#LicenseFileText").val("");
}
});
}
};
var blob = file.slice(start, stop + 1);
reader.readAsBinaryString(blob);
}
あなたは 'FileReader#readAsBinaryString'をポリフィルしましたか? IE11の[caniuse filereader](https://caniuse.com/#feat=filereader)のメモを読んでください。これは 'readAsBinaryString'をサポートしていません。デベロッパーツールコンソールにエラーがないという事実は謎です。おそらくあなたは正しい場所にエラーがないと見ています –
readAsBinaryStringの代替はありますか? – Raj
はい、[MDN Documentation](https://developer.mozilla.org/en/US/docs/Web/API/FileReader/readAsBinaryString)を読んでください。これは基本的に、標準から削除されたことを示しています。代わりに使用する必要があります –