問題を解決するために参照できる例を次に示します。ドラッグ アンド ドロップの方法とファイル チューザーの方法が示されています。スニペットのみを提供し、コンソールに表示される完全なエラーを提供しておらず、問題を示すjsfiddleがないため、コードをより具体的にすることはできません。
CSS
#progress_bar {
margin: 10px 0;
padding: 3px;
border: 1px solid #000;
font-size: 14px;
clear: both;
opacity: 0;
-moz-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
}
#progress_bar.loading {
opacity: 1.0;
}
#progress_bar .percent {
background-color: #99ccff;
height: auto;
width: 0;
}
#display {
width: 500px;
height: 150px;
}
div#drop_div {
width:400px;
height:300px;
background:#ccc;
border:10px dotted #aaa;
text-align:center;
font:22px Georgia;
color:#666;
line-height:300px;
}
HTML
<input type="file" id="files" name="file" />
<button id="cancel">Cancel read</button>
<ul id="list"></ul>
<div id="drop_div">Drag and drop a file here</div>
<div id="progress_bar">
<div class="percent">0%</div>
</div>
<div>Base64 encoded result</div>
<textarea id="display"></textarea>
Javascript
/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/*global global */
(function (global) {
"use strict";
var reader,
progress,
display,
iFiles,
bCancel,
dropZone,
filebox,
list;
function abortRead() {
if (reader) {
reader.abort();
}
}
function errorHandler(evt) {
switch (evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
alert("File Not Found!");
break;
case evt.target.error.NOT_READABLE_ERR:
alert("File is not readable");
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
alert("An error occurred reading this file.");
};
}
function updateProgress(evt) {
// evt is an ProgressEvent.
if (evt.lengthComputable) {
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
// Increase the progress bar length.
if (percentLoaded < 100) {
progress.style.width = percentLoaded + "%";
progress.textContent = percentLoaded + "%";
}
}
}
function read(file) {
var output = [];
output.push('<li><strong>', escape(file.name), '</strong> (', file.type || 'n/a', ') - ',
file.size, ' bytes, last modified: ',
file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() : 'n/a',
'</li>');
list.innerHTML = '<ul>' + output.join('') + '</ul>';
// Reset progress indicator on new file selection.
progress.style.width = "0%";
progress.textContent = "0%";
reader = new FileReader();
reader.onerror = errorHandler;
reader.onprogress = updateProgress;
reader.onabort = function (e) {
alert('File read cancelled');
};
reader.onloadstart = function (e) {
document.getElementById('progress_bar').className = 'loading';
};
reader.onload = function (e) {
// Ensure that the progress bar displays 100% at the end.
progress.style.width = "100%";
progress.textContent = "100%";
setTimeout("document.getElementById('progress_bar').className='';", 2000);
display.value = e.target.result;
}
// Read in the image file as a binary string.
reader.readAsDataURL(file);
}
function handleFileSelect(evt) {
read(evt.target.files[0]);
}
function handleFileDropSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
read(evt.dataTransfer.files[0]);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
global.addEventListener("load", function onLoad() {
global.removeEventListener("load", onLoad);
progress = document.querySelector(".percent");
display = document.getElementById("display");
iFiles = document.getElementById("files");
bCancel = document.getElementById("cancel");
dropZone = document.getElementById("drop_div");
filebox = document.getElementById("filebox");
list = document.getElementById("list");
dropZone.addEventListener("dragover", handleDragOver, false);
dropZone.addEventListener("drop", handleFileDropSelect, false);
iFiles.addEventListener("change", handleFileSelect, false);
bCancel.addEventListener("click", abortRead, false);
}, false);
}(window));
jsfiddleについて