vue tinymce将内容导出为word上传到服务器,并回显,完整代码
时间: 2023-12-17 15:05:35 浏览: 154
以下是使用vue-tinymce将内容导出为word并上传到服务器并回显的完整代码。
```html
<template>
<div>
<editor v-model="content" :init="tinymceInit"></editor>
<button @click="exportToWord">导出为Word</button>
<div v-if="wordUrl">
<a :href="wordUrl" download>下载Word文件</a>
<iframe :src="wordUrl"></iframe>
</div>
</div>
</template>
<script>
import VueEditor from 'vue-tinymce-editor'
export default {
components: { VueEditor },
data() {
return {
content: '',
tinymceInit: {
plugins: 'link image code',
toolbar: 'undo redo | bold italic | alignleft aligncenter alignright | code',
menubar: 'file edit view insert format tools table help',
images_upload_handler: (blobInfo, success, failure) => {
const formData = new FormData()
formData.append('file', blobInfo.blob(), blobInfo.filename())
axios.post('/api/upload/image', formData).then(response => {
success(response.data.url)
}).catch(() => {
failure('图片上传失败')
})
}
},
wordUrl: ''
}
},
methods: {
exportToWord() {
const html = this.content
const fileName = 'word-' + Date.now() + '.doc'
const blob = new Blob(['\ufeff' + html], { type: 'application/msword' })
const formData = new FormData()
formData.append('file', blob, fileName)
axios.post('/api/upload/word', formData).then(response => {
const url = response.data.url
this.wordUrl = url
})
}
}
}
</script>
```
注意替换以下代码中的`axios.post`函数的URL,以便将文件上传到您的服务器。
```javascript
axios.post('/api/upload/image', formData)
axios.post('/api/upload/word', formData)
```
阅读全文