我正在努力让上传者在 Jodit 工作。我遵循了文档/教程和几篇文章中给出的示例,但我总是收到错误“未捕获的语法错误:JSON.parse:JSON 数据的第 1 行第 1 列的意外字符”。
有人可以帮忙吗?
这是代码:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/public/css/jodit.min.css">
<title>Jodit</title>
</head>
<body>
<textarea class="textarea" name="editor" id="editor"></textarea>
<script src="/public/js/jodit.min.js"></script>
<script language="javascript" type="text/javascript">
var uploadOptions = {
enableDragAndDropFileToEditor: true,
uploader: {
url: '/public/fn/upload.php',
format: 'json', // Response format
data: {}, // Pass whatever post data you need here
isSuccess: function(resp) {
return !resp.error;
},
getMsg: function(resp) {
return resp.msg.join !== undefined ? resp.msg.join(' ') : resp.msg;
},
baseurl: '/public/images/blog', // Ex: /subdir/path/to/files/
process: function(resp) {
// Use custom response from upload to build expected object on success
let files = [];
resp.list.map((file) => {
files.push(file.name);
});
// Passes through to defaultHandlerSuccess as response
return {
files, // {array} The names of uploaded files. Field name uploader.filesVariableName
path: relativePathURL, // {string} Real relative path
baseurl: relativePathURL, // {string} Base url for filebrowser
error: (resp.success ? 0 : 1), // {int}
msg: resp.message // {string}
};
},
error: function(e) {
console.log(e);
},
defaultHandlerSuccess: function(resp) {
if (resp.files && resp.files.length) {
for (let i = 0; i < resp.files.length; i++) {
let full_file_path = resp.path + resp.files[i];
this.selection.insertImage(full_file_path, null, 250);
}
}
},
defaultHandlerError: function (resp) {
this.events.fire('errorPopap', [this.options.uploader.getMsg(resp)]);
}
}
}
var editor = new Jodit("#editor", uploadOptions);
</script>
</html>
以及用于移动上传文件的 php 文件:
if(isset($_FILES['files'])){
// Count # of uploaded files in array
$total = count($_FILES['files']['name']);
// Check if any files have been selected
if ($total > 0) {
// Default return array
$return_list = [];
// Loop through files
for ($i = 0; $i < $total; $i++) {
// Notice how the file data has been received
$file_data = array(
'name' => $_FILES['files']['name'][$i],
'tmp_name' => $_FILES['files']['tmp_name'][$i],
'size' => $_FILES['files']['size'][$i],
'type' => $_FILES['files']['type'][$i],
'error' => $_FILES['files']['error'][$i]
);
// Use whatever function to upload signle file
$file_response = handleUploadSingleFile($file_data);
// If response is true, add to array
if ($file_response) {
$return_list[] = $file_response;
}
}
// The return that is received on the Process function
return array(
'success' => true,
'message' => "Upload completed.",
"list" => $return_list
);
} else {
return array(
'success' => false,
'message' => "Please select file."
);
}
}