这实际上是可能的。
我已经放弃了。我确信我必须将文件上传到服务器,然后通过 ajax 将其发送回客户端,然后使用
用户,然后将其发送回服务器。
纯 Javascript
并非如此。我找到了这个链接:https://web.dev/read-files/
其中描述了以下方法:
<!-- Copyright 2018 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Read an image file</title>
<link rel="shortcut icon"
href="https://cdn.glitch.com/9b775a52-d700-4208-84e9-18578ee75266%2Ficon.jpeg?v=1585082912878">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<h1>
Read an image file
</h1>
<input type="file" id="file-selector">
<p id="status"></p>
<div>
<img id="output">
</div>
<script>
const status = document.getElementById('status');
const output = document.getElementById('output');
if (window.FileList && window.File && window.FileReader) {
document.getElementById('file-selector').addEventListener('change', event => {
output.src = '';
status.textContent = '';
const file = event.target.files[0];
if (!file.type) {
status.textContent = 'Error: The File.type property does not appear to be supported on this browser.';
return;
}
if (!file.type.match('image.*')) {
status.textContent = 'Error: The selected file does not appear to be an image.'
return;
}
const reader = new FileReader();
reader.addEventListener('load', event => {
output.src = event.target.result;
});
reader.readAsDataURL(file);
});
}
</script>
</body>
</html>
上面的代码将允许用户从他们的 HD 中选择一个图像,然后它使用 FileReader API 将数据加载到 javascript 中,而无需将其发送到服务器。我在其他地方读到过 FileReader 不能像这样通过网络工作,但我错了。
如果您想读取另一种文件,请确保在 FileReader 上调用不同的方法,例如 readAsText 用于文本,甚至作为数组缓冲区 readAsArrayBuffer。
该链接有更多详细信息。
2020 年 10 月 2 日在 Windows 10 上测试
- Firefox 81.0 64 位
- Chrome 85.0.4183.121(官方版本)(64 位)
- Edge 85.0.564.68(官方版本)(64 位)
反应
最简单的方法是使用 react-dropzone 库 https://github.com/react-dropzone/react-dropzone
以下来自官方文档 (MIT):
import React, {useCallback} from 'react'
import {useDropzone} from 'react-dropzone'
function MyDropzone() {
const onDrop = useCallback((acceptedFiles) => {
acceptedFiles.forEach((file) => {
const reader = new FileReader()
reader.onabort = () => console.log('file reading was aborted')
reader.onerror = () => console.log('file reading has failed')
reader.onload = () => {
// Do whatever you want with the file contents
const binaryStr = reader.result
console.log(binaryStr)
}
reader.readAsArrayBuffer(file)
})
}, [])
const {getRootProps, getInputProps} = useDropzone({onDrop})
return (
<div {...getRootProps()}>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
)
}