gpt4 book ai didi

javascript - 在javascript中获取上传文件的数据

转载 作者:IT王子 更新时间:2023-10-29 03:08:40 26 4
gpt4 key购买 nike

我想上传一个 csv 文件并处理该文件中的数据。最好的方法是什么?我不想使用 php 脚本。我做了以下步骤。但是这个方法只返回文件名而不是文件路径。所以我没有得到想要的输出。

<form id='importPfForm'>
<input type='file' name='datafile' size='20'>
<input type='button' value='IMPORT' onclick='importPortfolioFunction()'/>
</form>

function importPortfolioFunction( arg ) {
var f = document.getElementById( 'importPfForm' );
var fileName= f.datafile.value;
}

那么我怎样才能获取该文件中的数据呢?

最佳答案

以下示例基于 html5rocks 解决方案。它使用浏览器的 FileReader() 函数。仅限较新的浏览器。

参见 http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files

在这个例子中,用户选择了一个 HTML 文件。它显示在 <textarea> 中.

<form enctype="multipart/form-data">
<input id="upload" type=file accept="text/html" name="files[]" size=30>
</form>

<textarea class="form-control" rows=35 cols=120 id="ms_word_filtered_html"></textarea>

<script>
function handleFileSelect(evt) {
let files = evt.target.files; // FileList object

// use the 1st file from the list
let f = files[0];

let reader = new FileReader();

// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {

jQuery( '#ms_word_filtered_html' ).val( e.target.result );
};
})(f);

// Read in the image file as a data URL.
reader.readAsText(f);
}

document.getElementById('upload').addEventListener('change', handleFileSelect, false);
</script>

关于javascript - 在javascript中获取上传文件的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505333/

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