antd pro 表单添加富文本编辑器上传图片、视频(braft-editor)

引入相关

import BraftEditor from 'braft-editor'; // 引入编辑器组件
import 'braft-editor/dist/index.css'; // 引入编辑器样式
import { ContentUtils } from 'braft-utils';
import controls from '@/config/braft';//富文本编辑器菜单

注:Form.Item一定不要加name,否则值是无法回显的。

<Form.Item
            label="XXX"
            // name="XXX"
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 20 }}>
              <BraftEditor
              className="my-editor"
              controls={controls}
              placeholder="请输入正文内容"
              value={editorcontent}
              onChange={(editorState) => {
                setEditorcontent(editorState);
              }}
              extendControls={extendControlsContent}//自定义菜单(多媒体)
              />
            </Form.Item>
const uploadHandler = (info) => {
  if (info.file.status === 'done') {
    let data = info.file.response;
    if (data.code === 0) {
    info.file.type === "video/mp4"?
    setEditorcontent(
        ContentUtils.insertMedias(editorcontent, [
          {
            type: 'VIDEO',//视频是VIDEO写别的会导致视频无法显示
            url: 服务器返回地址,
          },
        ]),
      )
      :setEditorcontent(
        ContentUtils.insertMedias(editorcontent, [
          {
            type: 'IMAGE',
            url: 服务器返回地址,
          },
        ]),
      );
    } else {
      message.error('图片上传失败');
    }
  } else if (info.file.status === 'error') {
    message.error(`${info.file.name} file upload failed.`);
  }
};
const head = {Authorization:localStorage.getItem('Authorization'),}
const extendControlsContent = [
  {
    key: 'antd-uploader',
    type: 'component',
    component: (
      <Upload
        accept="*"
        showUploadList={false}
        onChange={uploadHandler}
        headers= {head}
        action={'服务器地址/image'}
      >
        <button type="button" className="control-item button upload-button" data-title="插入图片">
          上传图片
        </button>
      </Upload>
    ),
  },
   {
    key: 'antd-uploader1',//两个key值不可以相同,否则出不来
    type: 'component',
    component: (
      <Upload
        accept="*"
        showUploadList={false}
        onChange={uploadHandler}
        headers= {head}
        action={'服务器地址/video'}
      >
        <button type="button" className="control-item button upload-button" data-title="插入视频">
          上传视频
        </button>
      </Upload>
    ),
  },
];
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值