duanfu4446 2018-10-11 05:04
浏览 279

ckeditor5 vue检查已删除的图像

i am using ckeditor5 vue version for my article form from this package and it also come with example handling image upload by adding this code

UploadAdapter: function (loader) {
    this.loader = loader
    this.upload = () => {
        const body = new FormData();
        body.append('gambar', this.loader.file);

        let token = window.localStorage.getItem('token');

        return fetch('https://bkcuvue.test/api/v1/artikel/upload', {
            headers: {"Authorization": 'Bearer ' + token},
            body: body,
            method: 'POST'
        })
            .then(response => response.json())
            .then(downloadUrl => {
                return {
                    default: downloadUrl
                }
            })
            .catch(error => {
                console.log(error);
            });
    }
    this.abort = () => {
        console.log('Abort upload.')
    }
},

and then on my backend (laravel) is just like this

public function upload(Request $request)
    {
        if(!empty($request->gambar))
            $fileName = Helper::image_processing($this->imagepath,$this->width,$this->height,$request,'');
        else
            $fileName = '';

        return response()->json('/' . $this->imagepath . $fileName . '.jpg');
    }

to the ckeditor component

<vue-ckeditor type="classic" v-model="htmlContent" :upload-adapter="UploadAdapter"></vue-ckeditor>

and it works! but the problem that soon i realize is because this method is uploading the image to the server and then returning image path to or editor which will be saved to the database as a plain html tags...

then if for example i upload image and then i want to remove or maybe change the image then the editor will delete the img tag from the editor but not the real image file on the server....

the image file will remain on the server and there is no way i can delete it unless i select the image and delete it myself by going into server by ftp or ssh manualy.

is there any way to check if the image tag is deleted then it will delete on the server. Or maybe there is a way in backend to check if this image file is not used by any article...

and also if i upload the same image twice then it will also uploading to the server twice the same file, how can i detect if the same image file is already on the server and not upload duplicate but immediately return the image path

  • 写回答

1条回答 默认 最新

  • douyangquan2474 2018-10-29 22:50
    关注

    I use this method, upload the images and save the URLs. When I send the content of editor also send the URLs and on the server i compare that the URLs are in the content of the editor and those that are not i delete the corresponding file.

    Client:

    onChange={ ( event, editor ) => {
    
      this.state.description = editor.getData();
    
      var vm = this
    
      var urls = Array.from( new DOMParser().parseFromString( editor.getData(), 'text/html' )
                 .querySelectorAll( 'img' ) )
                 .map( img => img.getAttribute( 'src' ) );
    
      urls.forEach(function(element) {
        if (String(element).length > 10) {
          if (vm.state.filesCKeditor.length > 0) {
            if(filter(vm.state.filesCKeditor, function(o) { return String(o) === String(element) }).length == 0)
             {
               vm.state.filesCKeditor.push(element)
             }
           }else{
             vm.state.filesCKeditor.push(element)
           }
         }
       });
    }}
    

    enter image description here

    Server:

    foreach (explode(',',$this->post('filesCKeditor')) as $value) {
      # code...
      if (!isset(explode($value,$this->post('content'))[1])) {
        print_r('false');//add code for delete image
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集