阅读csv文件客户端 [英] Read csv file client side

查看:136
本文介绍了阅读csv文件客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个preVIEW用户上传大文件之前先阅读第一夫妇present csv文件客户端的行。

I have to read the first couple of lines of a csv file client side to present the user with a preview before uploading a large file.

我使用C#与ASP.Net(VS2010)

I'm using c# with ASP.Net (VS2010)

有谁知道如何可以做到这一点?此外,它适用于所有的浏览器!?!

Does anyone know how this can be done? Also that it works in all browsers!?!

(这是不可能的使用ActiveX左右,我们不希望我们的客户能够安装的东西!)

(It's not possible to use activeX or so, we do not want our clients to install something!)

一个样本code将是伟大的!

A sample code would be great!

在此先感谢!

推荐答案

3.6+火狐和Chrome(至少6版,可能是旧版本)支持的文件API,它可以让你从一个文件中输入读取本地文件

Firefox 3.6+ and Chrome (at least version 6, possibly older versions) support the File API which lets you read local files from a file input.

下面是一个简单的例子:

Here's a quick sample:

function handleFile(file) {
    var reader = new FileReader();
    reader.onload = function(e) {
        // Here's where you would parse the first few lines of the CSV file
        console.log(e.target.result);
    };
    reader.readAsText(file);
}

在你的HTML,你有这样的事情:

in your html, you would have something like this:

<input type="file" onchange="handleFile(this.files[0])" />

当然,在现实生活中,你应该让优雅降级。

Of course, in real life you should make it degrade gracefully.

文件对象名称键入属性你可以用它来验证它,如果你想更加严格的CSV文件。

The file object has name and type properties that you could use to verify that it's a CSV file if you wanted to be more strict.

这篇关于阅读csv文件客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆