tinymce将内容转换为word上传到后台转换为html文件,完整代码
时间: 2023-12-16 14:03:35 浏览: 116
以下是一个使用 TinyMCE 编辑器,将内容转换为 Word 文件并上传到后台,最后再将 Word 文件转换为 HTML 文件的完整代码:
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>TinyMCE to Word to HTML</title>
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js"></script>
<script>
tinymce.init({
selector: '#mytextarea',
plugins: 'wordcount',
toolbar: 'undo redo | formatselect | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | wordcount',
menubar: false,
setup: function (editor) {
editor.on('change', function () {
tinyMCE.triggerSave();
});
}
});
function convertToWord() {
var content = document.getElementById("mytextarea").value;
var fileName = "mydocument.docx";
// Create a new blob object
var blob = new Blob([content], {
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
// Create a new FormData object
var formData = new FormData();
formData.append('file', blob, fileName);
// Send the file to the server
$.ajax({
url: 'upload.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (response) {
if (response.status == "success") {
// Convert the Word file to HTML
convertToHTML(response.filePath);
} else {
alert(response.message);
}
},
error: function (xhr, status, error) {
alert("Error: " + error);
}
});
}
function convertToHTML(filePath) {
var formData = new FormData();
formData.append('filePath', filePath);
// Send the file path to the server
$.ajax({
url: 'convert.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (response) {
if (response.status == "success") {
// Display the HTML content
document.getElementById("myhtml").innerHTML = response.content;
} else {
alert(response.message);
}
},
error: function (xhr, status, error) {
alert("Error: " + error);
}
});
}
</script>
</head>
<body>
<textarea id="mytextarea"></textarea>
<button onclick="convertToWord()">Convert to Word</button>
<div id="myhtml"></div>
</body>
</html>
```
PHP 代码(upload.php):
```php
<?php
if ($_FILES['file']['error'] == UPLOAD_ERR_OK) {
$fileName = $_FILES['file']['name'];
$fileTmp = $_FILES['file']['tmp_name'];
$filePath = 'uploads/' . $fileName;
move_uploaded_file($fileTmp, $filePath);
echo json_encode(array(
'status' => 'success',
'filePath' => $filePath
));
} else {
echo json_encode(array(
'status' => 'error',
'message' => 'Error uploading file'
));
}
?>
```
PHP 代码(convert.php):
```php
<?php
$filePath = $_POST['filePath'];
$htmlPath = 'uploads/' . pathinfo($filePath, PATHINFO_FILENAME) . '.html';
if (file_exists($filePath)) {
$word = new COM("Word.Application") or die("Unable to start Word");
$doc = $word->Documents->Open(realpath($filePath));
$doc->SaveAs(realpath($htmlPath), 8);
$doc->Close();
$word->Quit();
echo json_encode(array(
'status' => 'success',
'content' => file_get_contents($htmlPath)
));
} else {
echo json_encode(array(
'status' => 'error',
'message' => 'File not found'
));
}
?>
```
请注意,这些代码需要在支持 COM 对象的 Windows 环境下运行,因为它们使用了 Microsoft Word 的 COM 接口。如果您运行这些代码时遇到问题,请确保您已经安装了 Microsoft Word 并且 PHP 配置中启用了 COM 支持。