node.js - 为什么这反应js文件上传器不起作用?
问题描述
我正在尝试从我的反应前端将图像文件发送到我的后端 express.js api。但是在我的 console.log 中,我只是在后端得到一个看起来像 {} 的空对象。react.js api调用有什么问题,不会让我在后端的console.log中看到文件。
//后端API
app.post('/api/v1/media', function (req, res) {
console.log(req.body);
res.json('api called');
});
//React JS api call
const files = e.target.files;
console.log(files);
setLoading(true);
const res = await fetch(
'http://localhost:4000/api/v1/media',
{
method:"POST",
headers: {'Content-Type':'application/json'},
body: JSON.stringify(files[0])
}`enter code here`
)
const file = await res.json();
解决方案
推荐阅读
- c# - 是否有任何 Linq 运算符在 'where' 不产生任何元素后执行?
- tabulator - 为什么我的制表器自定义标题过滤器在 keyup 上发生变化?
- python - TypeError: '(slice(None, None, None), 0)' 是 Keras 的无效键
- powershell - 在 Get-VisualStudioTemplate 的对象上找不到属性“FullName”
- r - 尝试从 R 中的数据框创建条形图
- stenciljs - Stenciljs:旋转木马第三方库在我更改范围时被破坏:阴影的“真”:“真”
- android - 我可以在不发布到 Play 商店的情况下测试动态模块吗?
- angular - Angular 9:TypeScript 中的 i18n
- java - 将布尔值集成到数组参数中
- python - 有没有办法知道一个对象内部是否有一个不可迭代的 NoneType 对象