Browse Source

添加文件上传

zhangbiao 4 years ago
parent
commit
7d17912074
1 changed files with 51 additions and 7 deletions
  1. 51 7
      src/components/tinymce/TinymceEditor.vue

+ 51 - 7
src/components/tinymce/TinymceEditor.vue

@@ -1,12 +1,6 @@
 <template>
   <div class="tinymce-editor">
-    <Editor
-      :id="tinymceId"
-      :init="init"
-      :disabled="disabled"
-      v-model="myValue"
-      @onClick="onClick"
-    ></Editor>
+    <Editor :id="tinymceId" :init="init" :disabled="disabled" v-model="myValue" @onClick="onClick"></Editor>
   </div>
 </template>
 
@@ -126,6 +120,56 @@ export default {
               success(res.data.url);
             });
           }
+        },
+        file_picker_callback: function(callback, value, meta, failure) {
+          //文件分类
+          var filetype =
+            ".pdf, .txt, .zip, .rar, .7z, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3, .mp4";
+          //后端接收上传文件的地址
+          var upurl = "/demo/upfile.php";
+          //为不同插件指定文件类型及后端地址
+          switch (meta.filetype) {
+            case "image":
+              filetype = ".jpg, .jpeg, .png, .gif";
+              upurl = "upimg.php";
+              break;
+            case "media":
+              filetype = ".mp3, .mp4";
+              upurl = "upfile.php";
+              break;
+            case "file":
+            default:
+          }
+          //模拟出一个input用于添加本地文件
+          var input = document.createElement("input");
+          input.setAttribute("type", "file");
+          input.setAttribute("accept", filetype);
+          input.click();
+          input.onchange = function() {
+            var file = this.files[0];
+
+            var xhr, formData;
+            console.log(file.name);
+            xhr = new XMLHttpRequest();
+            xhr.withCredentials = false;
+            xhr.open("POST", upurl);
+            xhr.onload = function() {
+              var json;
+              if (xhr.status != 200) {
+                failure("HTTP Error: " + xhr.status);
+                return;
+              }
+              json = JSON.parse(xhr.responseText);
+              if (!json || typeof json.location != "string") {
+                failure("Invalid JSON: " + xhr.responseText);
+                return;
+              }
+              callback(json.location);
+            };
+            formData = new FormData();
+            formData.append("file", file, file.name);
+            xhr.send(formData);
+          };
         }
       }
     };