上传PDF文件到Express服务器
Upload PDF file to Express server
我构建了一个基本的浏览器表单,允许用户上传 PDF 文件。然后我想将该文件发送到 Express 后端。看起来这应该是一个非常基本的操作,但我不熟悉端到端的过程,所以我不确定哪一部分失败了。我搜索了很多 SO questions/answers,但没有找到任何提供完整解决方案的,而且我也无法拼凑出一个解决方案。
更新: 看起来文件正在发送到服务器,但是编码有问题。我的猜测是 FileReader.readAsText
是错误的使用方法。 FileReader.readAsBinaryString
让我更接近一点,但仍然不太正确(并且已弃用)。 FileReader.readAsArrayBuffer
似乎是可行的方法,但我不确定如何在 Express 中正确处理缓冲区。
Client/Browser
该表单是在 React 中构建的,仅在输入本身上使用 onChange
处理程序。添加文件后,处理程序读取文件,将其添加到表单数据并向服务器发送 post 请求。
// React form
<input
name="upload"
onChange={this._handleUpload}
type="file"
/>
_handleUpload = (e) => {
const { files, name } = e.target;
// Read the file
const reader = new FileReader();
reader.onload = (e) => {
const file = e.target.result;
// Now that we have the file's contents, append to the form data.
const formData = new FormData();
formData.append('file', file);
formData.append('type', name);
axios
.post('/upload', formData)
.then(res => {
// Handle the response...
})
.catch(err => console.log(err));
};
// Reading as text. Should this be something else?
reader.readAsText(files[0]);
}
快递应用
快递应用使用multer中间件处理上传:
const app = express();
const upload = multer({});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.post('/upload', upload.any(), handleUpload);
中间件
最后,我有了自己的中间件,可以从 multer 获取文件。我通过将收到的文件写入磁盘来测试这篇文章。它有内容,但不是可读的 PDF 文件。
const handleUpload = (req, res, next) => {
// The file shows up on req.body instead of req.file, per multer docs.
const { file } = req.body;
// File is written, but it's not a readable PDF.
const tmp = fs.writeFileSync(
path.join(__dirname, './test.pdf'),
file,
);
}
我在这里有明显错误的部分吗?例如:PDF 是否需要以特殊方式处理?关于我的调试重点在哪里的任何提示?
看看是否能解决您的问题。
_handleUpload = (e) => {
const dataForm = new FormData();
dataForm.append('file', e.target.files[0]);
axios
.post('http://localhost:4000/test', dataForm)
.then(res => {
})
.catch(err => console.log(err));
}
render() {
return (
<div className="App">
<input
onChange={this._handleUpload}
type="file"
/>
</div>
)
}
服务器:
router.post('/test', upload.any(), (req, res) => {
console.log(req.files)
res.send({sucess: true})
})
无需发送文件类型,multer 会为您识别名称和类型。
我构建了一个基本的浏览器表单,允许用户上传 PDF 文件。然后我想将该文件发送到 Express 后端。看起来这应该是一个非常基本的操作,但我不熟悉端到端的过程,所以我不确定哪一部分失败了。我搜索了很多 SO questions/answers,但没有找到任何提供完整解决方案的,而且我也无法拼凑出一个解决方案。
更新: 看起来文件正在发送到服务器,但是编码有问题。我的猜测是 FileReader.readAsText
是错误的使用方法。 FileReader.readAsBinaryString
让我更接近一点,但仍然不太正确(并且已弃用)。 FileReader.readAsArrayBuffer
似乎是可行的方法,但我不确定如何在 Express 中正确处理缓冲区。
Client/Browser
该表单是在 React 中构建的,仅在输入本身上使用 onChange
处理程序。添加文件后,处理程序读取文件,将其添加到表单数据并向服务器发送 post 请求。
// React form
<input
name="upload"
onChange={this._handleUpload}
type="file"
/>
_handleUpload = (e) => {
const { files, name } = e.target;
// Read the file
const reader = new FileReader();
reader.onload = (e) => {
const file = e.target.result;
// Now that we have the file's contents, append to the form data.
const formData = new FormData();
formData.append('file', file);
formData.append('type', name);
axios
.post('/upload', formData)
.then(res => {
// Handle the response...
})
.catch(err => console.log(err));
};
// Reading as text. Should this be something else?
reader.readAsText(files[0]);
}
快递应用
快递应用使用multer中间件处理上传:
const app = express();
const upload = multer({});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.post('/upload', upload.any(), handleUpload);
中间件
最后,我有了自己的中间件,可以从 multer 获取文件。我通过将收到的文件写入磁盘来测试这篇文章。它有内容,但不是可读的 PDF 文件。
const handleUpload = (req, res, next) => {
// The file shows up on req.body instead of req.file, per multer docs.
const { file } = req.body;
// File is written, but it's not a readable PDF.
const tmp = fs.writeFileSync(
path.join(__dirname, './test.pdf'),
file,
);
}
我在这里有明显错误的部分吗?例如:PDF 是否需要以特殊方式处理?关于我的调试重点在哪里的任何提示?
看看是否能解决您的问题。
_handleUpload = (e) => {
const dataForm = new FormData();
dataForm.append('file', e.target.files[0]);
axios
.post('http://localhost:4000/test', dataForm)
.then(res => {
})
.catch(err => console.log(err));
}
render() {
return (
<div className="App">
<input
onChange={this._handleUpload}
type="file"
/>
</div>
)
}
服务器:
router.post('/test', upload.any(), (req, res) => {
console.log(req.files)
res.send({sucess: true})
})
无需发送文件类型,multer 会为您识别名称和类型。