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

Din*_*air 42 html javascript ajax file-upload

我想上传一个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;   
}
Run Code Online (Sandbox Code Playgroud)

那么如何才能获得该文件中的数据?

And*_*phy 45

以下示例基于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) {
    var files = evt.target.files; // FileList object

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

    var 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>
Run Code Online (Sandbox Code Playgroud)


Woo*_*ody 23

您可以使用新的HTML 5文件api来读取文件内容

https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications

但这不适用于每个浏览器,因此您可能需要服务器端回退.


Mar*_*ijk 20

下面的例子展示了FileReader读取上传文件内容的基本用法。这是此示例的工作 Plunker。

function init() {
  document.getElementById('fileInput').addEventListener('change', handleFileSelect, false);
}

function handleFileSelect(event) {
  const reader = new FileReader()
  reader.onload = handleFileLoad;
  reader.readAsText(event.target.files[0])
}

function handleFileLoad(event) {
  console.log(event);
  document.getElementById('fileContent').textContent = event.target.result;
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>

<head>
  <script src="script.js"></script>
</head>

<body onload="init()">
  <input id="fileInput" type="file" name="file" />
  <pre id="fileContent"></pre>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)


End*_*ess 14

Blob 本身存在一些新工具,您可以使用它们来读取文件内容,这样您就不必使用旧版 FileReader

// What you need to listen for on the file input
function fileInputChange (evt) {
  for (let file of evt.target.files) {
    read(file)
  }
}

async function read(file) {
  // Read the file as text
  console.log(await file.text())
  // Read the file as ArrayBuffer to handle binary data
  console.log(new Uint8Array(await file.arrayBuffer()))
  // Abuse response to read json data
  console.log(await new Response(file).json())
  // Read large data chunk by chunk
  console.log(file.stream())
}

read(new File(['{"data": "abc"}'], 'sample.json'))
Run Code Online (Sandbox Code Playgroud)