1

我对 HTML5 非常不熟悉,而且,在这里可以找到 jsp excel 阅读器: https ://github.com/Niggler/js-xls

好吧,据我了解,投递箱需要HTML5,而我正在使用的浏览器不支持HTML5。(我的客户也没有。如果可以的话,我会告诉他们将浏览器更新为更现代的东西)

所以我现在关心的是将文件下拉框转换为文件浏览表单,例如:

<form enctype="multipart/form-data" 
  action="action" method="post">
  <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
  <table width="600">
  <tr>
  <td>Names file:</td>
  <td><input type="file" name="file" /></td>
  <td><input type="submit" value="Upload" /></td>
  </tr>
  </table>
  </form>

这是具有 HTML5 框的 index.html。有人可以协助将盒子翻译成文件浏览器吗?让它做同样的事情吗?

<!DOCTYPE html>
<style>
#drop{
border:2px dashed #bbb;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
padding:25px;
text-align:center;
font:20pt bold,"Vollkorn";color:#bbb
}
#b64data{
width:100%;
}
</style>
<b>JS-XLS Live Demo</b><br />
<input type="radio" name="format" value="csv" checked> CSV<br>
<input type="radio" name="format" value="json"> JSON<br>
<input type="radio" name="format" value="form"> FORMULAE<br>

<div id="drop">Drop an XLS file here to see sheet data.</div>
<!--
<textarea id="b64data">... or paste a base64-encoding here</textarea>
<input type="button" id="dotext" value="Click here to process the base64 text" onclick="b64it();"/>
-->
<pre id="out"></pre>
<br />
<script src="xls.js"></script>
<script>
function get_radio_value( radioName ) {
var radios = document.getElementsByName( radioName );
for( var i = 0; i < radios.length; i++ ) {
if( radios[i].checked ) {
return radios[i].value;
}
}
}

function to_json(workbook) {
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var roa = XLS.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if(roa.length > 0){
result[sheetName] = roa;
}
});
return result;
}

function to_csv(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
var csv = XLS.utils.make_csv(workbook.Sheets[sheetName]);
if(csv.length > 0){
result.push("SHEET: " + sheetName);
result.push("");
result.push(csv);
}
});
return result.join("\n");
}

function to_formulae(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
var formulae = XLS.utils.get_formulae(workbook.Sheets[sheetName]);
if(formulae.length > 0){
result.push("SHEET: " + sheetName);
result.push("");
result.push(formulae.join("\n"));
}
});
return result.join("\n");
}

var tarea = document.getElementById('b64data');
function b64it() {
var cfb = XLS.CFB.read(tarea.value, {type: 'base64'});
var wb = XLS.parse_xlscfb(cfb);
process_wb(wb);
}

function process_wb(wb) {
var output = "";
switch(get_radio_value("format")) {
case "json":
output = JSON.stringify(to_json(wb), 2, 2);
break;
case "form":
output = to_formulae(wb);
break;
default:
output = to_csv(wb);
}
if(out.innerText === undefined) out.textContent = output;
else out.innerText = output;
}

var drop = document.getElementById('drop');
function handleDrop(e) {
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer.files;
var i,f;
for (i = 0, f = files[i]; i != files.length; ++i) {
var reader = new FileReader();
var name = f.name;
reader.onload = function(e) {
var data = e.target.result;
var cfb = XLS.CFB.read(data, {type: 'binary'});
//var arr = String.fromCharCode.apply(null, new Uint8Array(data));
//var cfb = XLS.CFB.read(btoa(arr), {type: 'base64'});
var wb = XLS.parse_xlscfb(cfb);
process_wb(wb);
};
reader.readAsBinaryString(f);
//reader.readAsArrayBuffer(f);
}
}

function handleDragover(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}

if(drop.addEventListener) {
drop.addEventListener('dragenter', handleDragover, false);
drop.addEventListener('dragover', handleDragover, false);
drop.addEventListener('drop', handleDrop, false);
}
</script>
4

1 回答 1

0

问题不在于拖放。问题是,如果浏览器对 HTML5 的支持不够好,它可能不支持 File API(给定代码中的 FileReader 对象和相关内容),因此它根本无法读取本地文件,即使它们使用老式文件浏览器选择。老实说,除了将带有文件的操作移至服务器端之外,我没有看到简单的解决方案。

于 2013-07-08T03:36:23.803 回答