gpt4 book ai didi

javascript - 使用 JavaScript for IE 在本地读取 CSV 文件,无需 FileReader API

转载 作者:行者123 更新时间:2023-12-02 17:01:13 25 4
gpt4 key购买 nike

我正在尝试在客户端读取 CSV 文件,我的代码可以在具有 FileAPI 的现代浏览器中正常工作,但是我需要找到一种支持 IE8 和 IE9 的方法。任何帮助表示赞赏。

使用的上传控件:

<input type="file" id ="uplaodFile"/>

JS:对于火狐:

var reader = new FileReader();
./.......
reader.readAsText(e.target.files.item(0));

对于 IE:??????

最佳答案

"Can you please elaborate on Ajax method to upload a file to server and echo it back as that is what i am looking at so that i dont cause any Security issues." -user428747

给定一个 .csv 文件,例如来自 Wikipedia - Comma-separated values - Example

csv

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00

html

<pre></pre>

js

$(function () {
var url = "file.csv";

$.getJSON("https://query.yahooapis.com/v1/public/yql?q=select"
+ "* from csv where url='" + url + "?'"
+ "&format=json&diagnostics=true&callback=?")
.then(function (data, textStatus, jqxhr) {
var arr = [];
var len = data.query.count;
$.each(data.query.results.row, function (k, v) {
$.map(v, function (val) {
if (val === null) {
val = '""';
};
arr.push(val)
});
});

var rebuild_csv = function () {
$.when(
$("pre")
.append(
arr.splice(0, len).join(",") + "\r\n"))
.done(function () {
if (arr.length) {
rebuild_csv()
};
});
};
rebuild_csv();

}, function (jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown)
});
});

jsfiddle http://jsfiddle.net/guest271314/2oLmxb0g/

另请参阅 How to get attachment file content with jQuery

关于javascript - 使用 JavaScript for IE 在本地读取 CSV 文件,无需 FileReader API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25692783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com