因為業(yè)務(wù)需求,上傳Word文件需要編輯,但如果使用Blob方式,在數(shù)據(jù)庫里存文件,就會造成格式消失。所以修改思路:上傳文件到服務(wù)器本地,保證數(shù)據(jù)存儲的完整性。
前端
<el-upload class="upload-demo" :action="item.fileUploadUrl" :show-file-list="false" :limit="1" :multiple="false" :auto-upload="true" :accept="item.accept" :before-upload="beforeUpload"
:headers="item.uploadHeaders" :data="item.uploadData">上傳
</el-upload></el-button>
fileActionList: [
{
width: 60,
name: "合同文件",
fileUploadUrl: "*************/importContract",
uploadData: {},
uploadHeaders: {},
accept: ".doc,.docx,.pdf",
},
],
beforeUpload(file, fileList) {
let promise = new Promise((resolve) => {
this.$nextTick(function () {
resolve(true);
});
});
this.$message.success("上傳成功");
return promise;
},
// 文件上傳操作
handleImport(row) {
this.fileActionList[0].uploadData = {
id: row.id,
};
this.fileActionList[0].uploadHeaders = {
Authorization: Cookie.get("*******"),
};
},
就是常規(guī)的傳文件
后端
@ApiOperation("導(dǎo)入合同文件")
@PostMapping("importContract")
public Result importData(@RequestParam(value="file") MultipartFile file, @RequestParam(value="id") Long id) throws Exception {
return projectService.importData(file, id);
}
@Override
public Result importData(MultipartFile file, Long id) throws Exception {
//獲取文件名
InputStream path = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
path = file.getInputStream();
fis = (FileInputStream) path;
File dir = new File("file/project");
if (!dir.exists()) {
dir.mkdirs();
}
String filePath = "file/project/" + id + "_" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
fos = new FileOutputStream(filePath);
int readlen = 0;
//字節(jié)數(shù)組,一次讀取8個字節(jié)
byte[] buf = new byte[8];
while ((readlen = fis.read(buf)) != -1) {
fos.write(buf, 0, readlen);
}
Project project = projectRepo.getById(id);
project.setContactFile(filePath);
projectRepo.save(project);
} catch (IOException e) {
log.error("" + e);
} finally {
path.close();
fis.close();
fos.close();
}
return Result.success("上傳成功");
}
這此采用的思路就是 把文件讀取后,寫入相對路徑,考慮到文件業(yè)務(wù)誤傳,服務(wù)器源文件不刪除,通過時間戳保證唯一性。存到服務(wù)器本地。文章來源:http://www.zghlxwxcb.cn/news/detail-581523.html
可以看到,最后格式都保留了下來
文章來源地址http://www.zghlxwxcb.cn/news/detail-581523.html
到了這里,關(guān)于Springboot + Vue 上傳Word、PDF文檔并保留內(nèi)部格式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!