Ada*_*dam 5 php tinymce laravel-5
我想在 TinyMCE 编辑器中上传图像。我在文档中找到了相关说明。
这些是我的 Javascript 设置:
tinymce.init({
selector: '#about',
images_upload_url: '/home/profile/about/img',
});
tinymce.activeEditor.uploadImages(function(success) {
$.post('/home/profile/about/img', tinymce.activeEditor.getContent()).done(function() {
console.log("Uploaded images and posted content as an ajax request.");
});
});
Run Code Online (Sandbox Code Playgroud)
我创建了以下路线来检查一切是否设置正确
Route::post('/home/profile/about/img', function(){
return json_encode(['location' => '/storage/app/public/pictures/bestAvatar.png' ]);
});
Run Code Online (Sandbox Code Playgroud)
我希望当我上传图像时不会上传任何图像并显示图像bestAvatar.png
- 但是我收到一个错误:
我错过了什么吗?是否可能是因为 tinymce ajax 调用中没有默认的 csrf 令牌?
我是这样解决的:
tinymce.init({
selector: '#about',
images_upload_handler: function (blobInfo, success, failure) {
var xhr, formData;
xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('POST', '/home/profile/about/img');
var token = '{{ csrf_token() }}';
xhr.setRequestHeader("X-CSRF-Token", token);
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;
}
success(json.location);
};
formData = new FormData();
formData.append('file', blobInfo.blob(), blobInfo.filename());
xhr.send(formData);
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8190 次 |
最近记录: |