KindEditor编辑器上传图片

html代码: 

<div class="form-group">
      <label>内容</label>
      <textarea name="content" rows="20" class="form-control"></textarea>
</div>

引入JS:

<script src="js/charts/flot/kindeditor.js"></script>
<script src="js/charts/flot/themes/default/default.css"></script>
<script src="js/charts/flot/lang/zh_CN.js"></script>

JS代码:

<script type="text/javascript">
        KindEditor.ready(function(K) {
            var csrf_token = '{{ csrf_token() }}';
            var editor1 = K.create('textarea[name="content"]', {
            cssPath : "js/charts/flot/plugins/code/prettify.css",
            uploadJson : "{{route('upload.news')}}",
            allowFileManager : true,//允许上传文件和图片
            filePostName : 'file',
            extraFileUploadParams : {'_token':csrf_token},
            afterCreate : function() {
            var self = this;
            K.ctrl(document, 13, function() {
              self.sync();
              K('form[name=example]')[0].submit();
            });
            K.ctrl(self.edit.doc, 13, function() {
              self.sync();
              K('form[name=example]')[0].submit();
            });
             }
        });
          
   });
</script>

后台php处理:

//特别需要注意的一点就是返回的error一定要用整数1或是0,别用字符串'1'或'0'
public function news(Request $request) {
    	$file = $request->file('file');
        if($file -> isValid()){
                $clientName = $file -> getClientOriginalName();
                $tmpName = $file ->getFileName();
                $realPath = $file -> getRealPath();
                $entension = $file -> getClientOriginalExtension();
                $newName = md5($clientName).".".$entension;
                $url_path = 'upload/banner/';
                $path = $file -> move($url_path,$newName);
                $pathName = json_decode($path,TRUE);
                $url1 = "http://12.12.12.12/newapi/uploadpic";
                $url3 = "http://13.13.13.13/newapi/uploadpic";
                $this->curl($path,$url1);
                $this->curl($path,$url3);
                $returnPath = '/upload/banner/'.$newName;
                return response()->json(['error'=>0,'url'=>$returnPath]);
        }
}


public function curl($path,$url){
        $curl = curl_init();
        if (class_exists('\CURLFile')) {
                curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
                $data = array('file' => new \CURLFile(realpath($path)));//>=5.5
        }else{
                if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
                }
                $data = array('file' => '@' . realpath($path));//<=5.5
        }

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1 );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
        $result = curl_exec($curl);
        $error = curl_error($curl);
    }

猜你喜欢

转载自blog.csdn.net/dabao87/article/details/82800201