【问题标题】:CORS is giving error when tinymce is integrated with react application当 tinymce 与反应应用程序集成时,CORS 出错
【发布时间】:2019-12-25 17:36:03
【问题描述】:

我已将 tinymce 与我的 React 应用程序集成。一切正常,但是当我上传图片时,CORS 出现以下错误:

plugin.min.js:9 从源“http://localhost:3000”访问“file:///C:/Users/Javeria/Documents/react/moonleaks/backend/media/images/”处的 XMLHttpRequest 已被阻止通过 CORS 策略:跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https。

export default class App extends Component {

handleEditorChange = (e) => {
console.log('Content was updated:', e.target.getContent());
}



render() {
return (
<Editor
initialValue="<p>This is the initial content of the editor</p>"
init={{
plugins: 'link image code',
toolbar: 'undo redo | bold italic | alignleft aligncenter alignright | 
code',

selector: 'textarea',  // change this value according to your html


images_upload_url:
'file:///C:/Users/Javeria/Documents/react/moonleaks/backend/media/images/',
images_upload_credentials: true
}}
onChange={this.handleEditorChange}
/>

);
}

Node server function:
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'media/images')
},
filename: (req, file, cb) => {
cb(null, file.originalname)
}
});
const upload = multer({ storage })

app.post('/upload', upload.single('image'), (req, res) => {
    if (req.file)
    res.json({
    imageUrl: `images/${req.file.filename}`
    , file_size : req.file});
    else 
    res.status("409").json("No Files to Upload.");
    });                             

我该如何纠正这个错误?

【问题讨论】:

  • 来自哪个库?
  • 从'@tinymce/tinymce-react'导入{编辑器};

标签: reactjs


【解决方案1】:

您不能直接将图像上传到系统的驱动器文件夹,您必须实现一个服务器来接收图像。

images_upload_url替换为服务器上的路径(可以在您的本地主机上运行)。

参考PHP文件上传处理程序Link

【讨论】:

  • 是的,我有一个在 url 上运行的节点服务器:localhost:4000。我也有图片上传功能,效果很好。您可以查看该功能,因为我在上面的帖子中编辑了代码。但是如何在 tinymce 中使用这个功能呢?
  • 然后尝试将images_upload_url 更改为localhost:4000/{path_of_upload_function}
  • 我尝试使用此路径:localhost:4000/upload。现在它给出 500(内部服务器错误)。我以这种方式在其他地方使用了这个函数: const uploaders = this.state.images.map(image => { const data = new FormData(); data.append("image", image, image.name); axios.post(BASE_URL + 'upload', data)}) 这里的 BASE_URL 是 'localhost:4000';
  • 这意味着服务器端代码有一些错误,请尝试在不同的问题中询问并详细解释错误。
猜你喜欢
  • 2020-08-12
  • 2012-12-11
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 2019-01-09
  • 2018-10-03
  • 2023-04-07
相关资源
最近更新 更多