tineMCE 的官方示例提供了前端上傳圖片方法 images_upload_handler 的寫法。
但官方寫的有點問題,上傳會報錯。
不過修改也很簡單:
images_upload_handler: function (blobInfo, success, failure) { var xhr, formData; xhr = new XMLHttpRequest(); xhr.withCredentials = false; xhr.open('POST', 'http://localhost/oasis_publish_test_php/index.php/upload/upImg'); xhr.onload = function() { var json; if (xhr.status < 200 || xhr.status >= 300) { failure('HTTP Error: ' + xhr.status); return; } json = JSON.parse(xhr.responseText); if (!json || typeof json.location != 'string') { failure('Invalid JSON: ' + xhr.responseText); return; } success(json.location); }; formData = new FormData(); formData.append('file', blobInfo.blob()); xhr.send(formData); }
標紅的部分就是修改的部分。