当前位置:网站首页>Using editor How to handle MD uploading pictures?
Using editor How to handle MD uploading pictures?
2022-06-23 04:49:00 【Mamba kids' shoe】
Recently, I used it in my new blog background Editor.md As an editor for publishing articles , Later, I encountered some problems when uploading pictures , Is not in the submitted form Token, Unable to get Laravel Of VerifyCsrfToken Middleware check . This article will explain how to solve this problem .
Editor.md The main features
- Support “ standard ”Markdown / CommonMark and Github Grammar of style , It can also be transformed into a code editor ;
- Support real-time preview 、 picture ( Cross domain ) Upload 、 Pre formatted text / Code / Table insert 、 Code folding 、 Search for replacement 、 read only mode 、 Custom style theme and multi language syntax highlighting ;
- Support ToC(Table of Contents)、Emoji expression 、Task lists、@ Links, etc. Markdown Extended syntax ;
- Support TeX Scientific formula ( be based on KaTeX)、 flow chart Flowchart and Sequence diagram Sequence Diagram;
- Support identification and parsing HTML label , And support custom filter label resolution , With reliable security and almost unlimited scalability ;
- Support AMD / CMD Modular loading ( Support Require.js & Sea.js), And support custom extensions ;
- Compatible with mainstream browsers (IE8+) and Zepto.js, And support iPad And so on ;
- Supports custom theme styles ;
Github Download address :https://github.com/pandao/editor.md
front end js To configure
/* To configure editormd */
var editor = editormd("editormd", {
path: "{
{ asset('/editor.md/lib/') }}",
height: 700,
syncScrolling: "single",
toolbarAutoFixed: false,
saveHTMLToTextarea: false,
imageUpload: true,
imageFormats : ["jpg","jpeg","gif","png","bmp","webp"],
imageUploadURL:"{
{url('backend/uploadimage')}}"
});Methods and routing
public function uploadimage(Request $request)
{
$message='';
if (!$this->disk->exists('/article')) {
$message = "article Folder does not exist , Please create first ";
}else{
$pathDir=date('Ymd');
if(!$this->disk->exists('/article/'.$pathDir)){
$this->disk->makeDirectory('/article/'.$pathDir);
}
}
if($request->file('editormd-image-file')){
$path="uploads/article/".$pathDir;
$pic = $request->file('editormd-image-file');
if($pic->isValid()){
$newName=md5(time() . rand(0, 10000)).".".$pic->getClientOriginalExtension();
if($this->disk->exists($path.'/'.$newName)){
$message = " The file name already exists or the file already exists ";
}else{
if($pic->move($path,$newName)){
$url = asset($path.'/'.$newName);
}else{
$message=" System exception , File save failed ";
}
}
}else{
$message = " The file is invalid ";
}
}else{
$message="Not File";
}
$data = array(
'success' => empty($message) ? 1 : 0, //1: Upload successful 0: Upload failed
'message' => $message,
'url' => !empty($url) ? $url : ''
);
header('Content-Type:application/json;charset=utf8');
exit(json_encode($data));
}Route::group(['prefix' => 'backend'], function () { Route::post('uploadimage',['uses'=>'Backend\[email protected]']); });
Upload error
We can see that 500 error , This is from Laravel Of TokenMismatchException Caused by abnormality , That is to say Laravel By default, the anti CSRF, and Editor.md The upload form of does not contain token, That's why this error occurs .
resolvent
When we click upload picture, the upload box will pop up , This is the uploaded form , We can use the debugger to find out that this is a request image-dialog.js, The specific path is editor.md/plugins/image-dialog/image-dialog.js. We can analyze the var dialogContent Variables are modified .
Specific modification code
if (settings.crossDomainUpload)
{
action += "&callback=" + settings.uploadCallbackURL + "&dialog_id=editormd-image-dialog-" + guid;
}
var csrfToken = $('meta[name="_token"]').attr('content');
var csrfField = "";
if (csrfToken) {
csrfField = "<input type='hidden' name='_token' value='" + csrfToken + "' />";
}- stay if(settings.crossDomainUpload) Add this when it's over 5 Line code , Next , take csrfField Variables are added to the following code .
var dialogContent = ( (settings.imageUpload) ? "<form action=\"" + action + "\" target=\"" + iframeName + "\" method=\"post\" enctype=\"multipart/form-data\" class=\"" + classPrefix + "form\">" : "<div class=\"" + classPrefix + "form\">" ) +
( (settings.imageUpload) ? "<iframe name=\"" + iframeName + "\" id=\"" + iframeName + "\" guid=\"" + guid + "\"></iframe>" : "" ) +
"<label>" + imageLang.url + "</label>" +
"<input type=\"text\" data-url />" + (function () {
return (settings.imageUpload) ? "<div class=\"" + classPrefix + "file-input\">" +
"<input type=\"file\" name=\"" + classPrefix + "image-file\" accept=\"image/*\" />" +
csrfField +
"<input type=\"submit\" value=\"" + imageLang.uploadButton + "\" />" +
"</div>" : "";
})() +
"<br/>" +
"<label>" + imageLang.alt + "</label>" +
"<input type=\"text\" value=\"" + selection + "\" data-alt />" +
"<br/>" +
"<label>" + imageLang.link + "</label>" +
"<input type=\"text\" value=\"http://\" data-link />" +
"<br/>" + csrfField +
( (settings.imageUpload) ? "</form>" : "</div>");- Then add this line of code to the header of the requested page
<meta name="_token" content="{
{ csrf_token() }}"/>In this way, you can upload the picture again token Take it to .
Link to the original text :http://blog.kesixin.xin/article/51
边栏推荐
- 第二次作业笔记
- 2022年起重机械安全管理考试题库及答案
- 396. 矿场搭建
- 【论文阅读】Semi-Supervised Learning with Ladder Networks
- Audio and video development journey (63) - animation and rendering of Lottie source code analysis
- 距离度量 —— 余弦距离(Cosine Distance)
- 在Pycharm中对字典的键值作更新时提示“This dictionary creation could be rewritten as a dictionary literal ”的解决方法
- win10查看my.ini路径
- Pta:7-60 pet growth
- 关于php里tcp通讯用swoole框架出现的小问题
猜你喜欢

Can bus Basics

20000 words + 20 pictures | details of nine data types and application scenarios of redis

Abnova 血液总核酸纯化试剂盒方案

DPR-34V/V双位置继电器

golang使用mongo-driver操作——增(基础)
![[multimode] unimo](/img/a5/a857e20e1432ef3623527c8655a49a.png)
[multimode] unimo

X24cxx series EEPROM chip C language universal reading and writing program

在Pycharm中对字典的键值作更新时提示“This dictionary creation could be rewritten as a dictionary literal ”的解决方法

使用Live Chat促进业务销售的惊人技巧

Halcon knowledge: binocular_ Discrimination knowledge
随机推荐
如何解决独立站多渠道客户沟通难题?这款跨境电商插件一定要知道!
DPR-34V/V双位置继电器
Summary of switched reluctance motor suspension drive ir2128
在Pycharm中对字典的键值作更新时提示“This dictionary creation could be rewritten as a dictionary literal ”的解决方法
[binary tree] completeness test of binary tree
composer按装laravel
在Pycharm中使用append()方法对列表添加元素时提示“This list creation could be rewritten as a list literal“的解决方法
2022年烷基化工艺考题及模拟考试
关于php里tcp通讯用swoole框架出现的小问题
Pta:6-71 clock simulation
What are the characteristics of SRM supplier management system developed by manufacturing enterprises
如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!
2 万字 + 20张图|细说 Redis 九种数据类型和应用场景
The spring recruitment in 2022 begins, and a collection of interview questions will help you
开关磁阻电机悬浮驱动IR2128小结
独立站聊天机器人有哪些类型?如何快速创建属于自己的免费聊天机器人?只需3秒钟就能搞定!
Cocos learning diary 2 - scripts and attributes
const理解之二
在PCB板边走高频高速信号线的注意事项–高频高速信号设计基本原则
thinkphp6 模版替换