reactjs - react froala wysiwyg-editor 中的图像上传参数
问题描述
我使用的是Froala WYSIWYG Editor的ReactJS 版本。当我使用图像上传功能时,我无法根据服务器请求获取参数。
这是配置:
this.config = {
// Set the image upload parameter.
imageUploadParam: 'image',
// Set the image upload URL.
imageUploadURL: apiUrl + "/api/v1/admin/upload/image",
// Additional upload params.
imageUploadParams: {
token: cookie.getItem('token'),
test_id: '11',
},
// Set request type.
imageUploadMethod: 'POST',
// Set max image size to 2MB.
imageMaxSize: 2 * 1024 * 1024,
// Allow to upload PNG and JPG.
imageAllowedTypes: ['jpeg', 'jpg', 'png', 'gif'],
}
上传图片时,我收到以下消息:
{"code":403,"message":"令牌无效!"}
我检查了请求条目:
console.log(request.body);
结果: {}
console.log(request.query);
结果: {}
console.log(request.params);
结果: {}
我错过了什么还是配置部分错了?
解决方案
Junaid Babatunde 写了一篇很棒的文章,链接如下: https ://medium.com/@junaidtunde1/angular-2-with-froala-text-editor-image-upload-to-remote-server-732ef2793356
默认图片上传被一个'beforeUpload'事件拦截,您可以将其发送到数据库,然后在回调中将图片(来自数据库)插入编辑器,原始副本在发送到数据库后被丢弃是然后插入到编辑器中的副本。
顺便说一句 - imageUpload: true 显然是需要的!
这是代码:
options: Object = {
charCounterCount: false,
placeholderText: 'Edit Your Content Here!',
imageUpload: true,
imageDefaultAlign: 'left',
imageDefaultDisplay: 'inline-block',
// Set max image size to 5MB.
imageMaxSize: 5 * 1024 * 1024,
// Allow to upload PNG and JPG.
imageAllowedTypes: ['jpeg', 'jpg', 'png'],
events: {
'froalaEditor.image.beforeUpload': function(e, editor, images) {
// Before image is uploaded
const data = new FormData();
data.append('image', images[0]);
axios.post('your_imgur_api_url', data, {
headers: {
'accept': 'application/json',
'Authorization': 'your_imgur_client_id/api_key',
'Accept-Language': 'en-US,en;q=0.8',
'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
}
}).then(res => {
editor.image.insert(res.data.data.link, null, null, editor.image.get());
}).catch(err => {
console.log(err);
});
return false;
}
}
};
推荐阅读
- c++ - 使用自上而下的方法最大化切割段?
- .htaccess - Aapche2 htaccess RewriteCond 与 ErrorDocument 404
- machine-learning - PyTorch 的 nn.Transformer 中的 src 和 tgt 有什么区别?
- r - R:计算向量中长度N个元素的均值和方差
- data-visualization - 画面自定义调色板..任何人都知道为什么这不起作用
- maven - SoapUI Project 在 Jenkins 传递依赖错误中使用 Maven 插件测试执行
- jersey - Jersey/JAX-RS WriterInterceptor 和 HTTP 状态
- c# - C#如何更改set访问器中值字段的类型?
- google-cloud-firestore - 通过python脚本将octetstream转换为json格式
- spring - 为什么我的 MockBeans 和 MockRestServiceServer 在 Spring Boot 中测试 JMS Listener 时没有返回正确的响应