Summernote image upload

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

I have problem with editor Summernote. I want to upload images into a catalog on server. I have some script:

    <script type="text/javascript"> $(function () {         $(\'.summernote\').summernote({                     height: 200                   });                      $(\'.summernote\').summernote({                        height:300,                        onImageUpload: function(files, editor, welEditable) {                         sendFile(files[0],editor,welEditable);                        }                       });              }); </script> <script type="text/javascript"> function sendFile(file, editor, welEditable) {     data = new FormData();     data.append("file", file);     url = "http://localhost/spichlerz/uploads";     $.ajax({         data: data,         type: "POST",         url: url,         cache: false,         contentType: false,         processData: false,         success: function (url) {             editor.insertImage(welEditable, url);         }     }); } </script>            <td><textarea class="summernote" rows="10" cols="100" name="tekst"></textarea></td>

Of course I have all js and css files. What i do wrong? If I click on image upload and go to the editor, the image is not in textarea.

If I delete sendFile function and onImageUpload: the image save on base64.

Link to summernote: http://hackerwins.github.io/summernote/

回答1:

I tested this code and Works

Javascript

<script>     $(document).ready(function() {         $('#summernote').summernote({             height: 200,             onImageUpload: function(files, editor, welEditable) {                 sendFile(files[0], editor, welEditable);             }         });         function sendFile(file, editor, welEditable) {             data = new FormData();             data.append("file", file);             $.ajax({                 data: data,                 type: "POST",                 url: "Your URL POST (php)",                 cache: false,                 contentType: false,                 processData: false,                 success: function(url) {                     editor.insertImage(welEditable, url);                 }             });         }     });   </script>

PHP

if ($_FILES['file']['name']) {             if (!$_FILES['file']['error']) {                 $name = md5(rand(100, 200));                 $ext = explode('.', $_FILES['file']['name']);                 $filename = $name . '.' . $ext[1];                 $destination = '/assets/images/' . $filename; //change this directory                 $location = $_FILES["file"]["tmp_name"];                 move_uploaded_file($location, $destinati  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!