公共只是发布修改 文件上传代码优化
This commit is contained in:
parent
40e439b1e5
commit
576df026a5
@ -156,22 +156,16 @@ const uploadNumber = ref<number>(0)
|
||||
const { uploadUrl, httpRequest } = useUpload()
|
||||
// 上传文件
|
||||
const upload = async (myFile:any) => {
|
||||
//获取当前id
|
||||
// console.log("id",fileNid.value)
|
||||
// console.log("myFile",myFile)
|
||||
// const iid = fileNid.value.toString()
|
||||
// const iid = fileNid.value.toString()//获取当前id
|
||||
const formData = new FormData();
|
||||
const token = getAccessToken();
|
||||
//设置配置
|
||||
const configs = {
|
||||
headers: { 'Authorization': `Bearer ${token}`,'Content-Type': 'multipart/form-data' }
|
||||
}
|
||||
//拿到的信息来加入form
|
||||
formData.append('file', myFile);
|
||||
// formData.append('fileId', iid.toString());
|
||||
formData.append('file', myFile);//拿到的信息来加入form
|
||||
try {
|
||||
const response = await axios.post('http://192.168.1.15:48080/admin-api/only/onlyof/update', formData, configs);
|
||||
console.log(response);
|
||||
await axios.post('http://192.168.1.15:48080/admin-api/only/onlyof/update', formData, configs);
|
||||
} catch (error) {
|
||||
console.error('Upload failed:', error);
|
||||
}
|
||||
@ -196,7 +190,6 @@ const SyncFiles = async () => {
|
||||
message.error("文件正在同步中!")
|
||||
}
|
||||
lod.value=true
|
||||
// console.log("fileList.value",fileList.value)
|
||||
//判断是上传列表是否为空
|
||||
if (fileList.value.length==0){
|
||||
message.error("同步文件为空! ")
|
||||
@ -205,16 +198,15 @@ const SyncFiles = async () => {
|
||||
for (const Name of fileList.value) {
|
||||
const fName = Name as UploadUserFile & {page?: number,lodTxt?: String};
|
||||
fName.lodTxt="10%"
|
||||
//判断是否全部上传
|
||||
if (fName.page==100){
|
||||
fName.lodTxt="完成"
|
||||
continue
|
||||
}
|
||||
//判断是否全部上传
|
||||
await delay(100);
|
||||
fName.page = 20;
|
||||
fName.lodTxt="20%"
|
||||
//获取文件名
|
||||
const fileName = fName.name.trim();
|
||||
const fileName = fName.name.trim();//获取文件名
|
||||
//设置要获取的数据
|
||||
try {
|
||||
const path = await getPage(fileName)
|
||||
@ -229,15 +221,12 @@ const SyncFiles = async () => {
|
||||
message.error("文件路径获取失败!")
|
||||
continue;
|
||||
}
|
||||
//替换所有路径\为/
|
||||
filePath = filePath.replace(/\\/g, "/");
|
||||
filePath = filePath.replace(/\\/g, "/"); //替换所有路径\为/
|
||||
await delay(100);
|
||||
fName.page = 50;
|
||||
fName.lodTxt="50%"
|
||||
//获取文件后缀名
|
||||
const fileExt = getFileExtension(fileName) || '';
|
||||
const fileExt = getFileExtension(fileName) || ''; //获取文件后缀名
|
||||
fName.page = 60;
|
||||
// console.log(fileExt)
|
||||
fName.lodTxt="60%"
|
||||
//更具对应后缀名获取对应接口
|
||||
const addMap = {
|
||||
@ -258,10 +247,8 @@ const SyncFiles = async () => {
|
||||
const add = addMap[fileExt] || ''; // 默认值为空字符串对应路径获取对应接口
|
||||
fName.page = 90;
|
||||
fName.lodTxt="90%"
|
||||
// console.log(`${config.ai_url}/${add}?path=${filePath}`)
|
||||
const response = await axios.post(`${config.ai_url}/${add}?path=${filePath}`);
|
||||
await axios.post(`${config.ai_url}/${add}?path=${filePath}`);
|
||||
upDing.value=false
|
||||
console.log("response",response)
|
||||
await delay(100);
|
||||
fName.page = 100;
|
||||
fName.lodTxt="完成"
|
||||
@ -284,12 +271,11 @@ const emitUpdatePage = () => {
|
||||
if (pa.page != null) {
|
||||
result.push(pa.page)
|
||||
}
|
||||
// console.log(result)
|
||||
}
|
||||
emit('update:fileStatus', result)
|
||||
}
|
||||
//设置延迟
|
||||
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); //设置延迟
|
||||
//获取文件后缀名
|
||||
const getFileExtension = (fileName: string) => {
|
||||
let fileLast: string | undefined;
|
||||
@ -332,11 +318,9 @@ const fileNid = ref(integer)
|
||||
// 编辑文件
|
||||
const edit = async (row) => {
|
||||
//原本想根据id来指定文件后来想不查看是否有重复文件
|
||||
// console.log("id",fileNid.value)
|
||||
// const iid = fileNid.value.toString()
|
||||
// let name = fileNid.value+row.file.name.trim()
|
||||
let name = row.file.name.trim()
|
||||
// console.log(name)
|
||||
queryParamsOnly.pageNo = 1
|
||||
queryParamsOnly.fileName=name
|
||||
//设置完查询的值去查询
|
||||
@ -356,7 +340,6 @@ const download = async (row) => {
|
||||
let name = row.file.name.trim()
|
||||
//拿到名字
|
||||
// const iid = fileNid.value.toString()
|
||||
// console.log("name",name)
|
||||
try {
|
||||
const token = getAccessToken();
|
||||
//拿文件状态
|
||||
@ -415,7 +398,6 @@ const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
|
||||
fileVerify.value = false;
|
||||
//判断是不是有相同名字文件
|
||||
for (let i = 0; i <fileList.value.length-1; i++){
|
||||
// console.log("list",fileList.value[i].name)
|
||||
const fileName = fileList.value[i].name
|
||||
if (resName===fileName){
|
||||
fileVerify.value = true
|
||||
@ -468,8 +450,7 @@ const excelUploadError: UploadProps['onError'] = (): void => {
|
||||
const handleRemove = (file: UploadFile) => {
|
||||
const index = fileList.value.map((f) => f.name).indexOf(file.name)
|
||||
if (index > -1) {
|
||||
//删除文件链接已选择删除的链接索引
|
||||
fileList.value.splice(index, 1)
|
||||
fileList.value.splice(index, 1) //删除文件链接已选择删除的链接索引
|
||||
MysqlUrl.value.splice(index,1)
|
||||
const updatedFileStatus = [...props.fileStatus];
|
||||
updatedFileStatus.splice(index, 1);
|
||||
@ -488,7 +469,6 @@ const handleView = (file)=> {
|
||||
console.error("文件不可预览");
|
||||
}
|
||||
const fileExt = url.substring(url.length-5,url.length).trim().toLowerCase()
|
||||
// console.log(fileExt)
|
||||
if (fileExt==="docx"||fileExt==="xlsx"||fileExt==="pptx"){
|
||||
window.open("https://view.officeapps.live.com/op/view.aspx?src="+encodeURIComponent(url),'_blank')
|
||||
}else if(fileExt===".txt"||fileExt===".pdf"){
|
||||
@ -498,44 +478,24 @@ const handleView = (file)=> {
|
||||
|
||||
//文件下载时文件名为原来名字
|
||||
const downloadFile = async (file)=> {
|
||||
// console.log("file",file)
|
||||
//使用 fetch API 请求文件并返回 Blob 数据
|
||||
const response = await fetch(file.url);
|
||||
//获取 response blob 属性
|
||||
const blob = await response.blob();
|
||||
//创建一个 <a> 元素
|
||||
const link = document.createElement('a')
|
||||
//创建一个临时的对象 URL,指向该 Blob。
|
||||
link.href = URL.createObjectURL(blob);
|
||||
//指定下载时的文件名,此时浏览器将强制使用这个名称。
|
||||
link.download = file.name
|
||||
//添加下载link
|
||||
document.body.appendChild(link);
|
||||
//手动触发点击事件,开始下载文件。
|
||||
link.click();
|
||||
//移除下载link
|
||||
document.body.removeChild(link);
|
||||
//下载完成后,释放创建的对象 URL,避免内存泄漏
|
||||
URL.revokeObjectURL(link.href);
|
||||
const response = await fetch(file.url); //使用 fetch API 请求文件并返回 Blob 数据
|
||||
const blob = await response.blob(); //获取 response blob 属性
|
||||
const link = document.createElement('a') //创建一个 <a> 元素
|
||||
link.href = URL.createObjectURL(blob); //创建一个临时的对象 URL,指向该 Blob。
|
||||
link.download = file.name //指定下载时的文件名,此时浏览器将强制使用这个名称。
|
||||
document.body.appendChild(link); //添加下载link
|
||||
link.click(); //手动触发点击事件,开始下载文件。
|
||||
document.body.removeChild(link); //移除下载link
|
||||
URL.revokeObjectURL(link.href); //下载完成后,释放创建的对象 URL,避免内存泄漏
|
||||
}
|
||||
|
||||
const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
|
||||
console.log(uploadFile)
|
||||
}
|
||||
//watch 传过来的id又没有改动 有的话重新赋值给filenid
|
||||
// watch(
|
||||
// () => props.fileId,
|
||||
// (val:number) => {
|
||||
// // console.log("valFileId",val)
|
||||
// // fileNid.value=val
|
||||
// },
|
||||
// { immediate: true, deep: true }
|
||||
// )
|
||||
// 监听模型绑定值变动
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(val: string | string[]) => {
|
||||
// console.log("val",val)
|
||||
if (!val) {
|
||||
fileList.value = [] // fix:处理掉缓存,表单重置后上传组件的内容并没有重置
|
||||
return
|
||||
@ -562,8 +522,6 @@ watch(
|
||||
...(val as string[]).map((url) => ({ name: url,url: url}))
|
||||
);
|
||||
}
|
||||
// console.log("fileList", fileList.value)
|
||||
// console.log("MysqlUrl", MysqlUrl.value)
|
||||
},
|
||||
{ immediate: true, deep: true }
|
||||
)
|
||||
@ -584,7 +542,6 @@ watch(
|
||||
fileList.value[index].lodTxt = "完成"
|
||||
}
|
||||
});
|
||||
// console.log("fileList int",fileList.value)
|
||||
},
|
||||
{ immediate: true, deep: true }
|
||||
)
|
||||
@ -596,7 +553,6 @@ const emitUpdateModelValue = () => {
|
||||
if (props.limit === 1 || isString(props.modelValue)) {
|
||||
result = result.join(',')
|
||||
}
|
||||
// console.log("result",result)
|
||||
emit('update:modelValue', result)
|
||||
}
|
||||
|
||||
@ -608,7 +564,6 @@ const emitUpdateModelValue2 = () => {
|
||||
if (props.limit === 1 || isString(props.modelValue)) {
|
||||
result = result.join(',')
|
||||
}
|
||||
// console.log("result",result)
|
||||
emit('update:modelValue', result)
|
||||
// 多了一步就是清空就文件链接以便于再次删除后不会出现重复
|
||||
MysqlUrl.value = []
|
||||
|
@ -33,7 +33,7 @@
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="24">
|
||||
<el-col :span="18">
|
||||
<el-form-item label="知识分类" prop="typeId">
|
||||
<!-- <el-input v-model="formData.typeId" placeholder="请选择知识分类" />-->
|
||||
<el-tree-select
|
||||
|
Loading…
Reference in New Issue
Block a user