Commit ebf76af9 authored by xinzhedeai's avatar xinzhedeai

fetch上传图片

parent c52db27e
......@@ -104,8 +104,9 @@
:before-upload="beforeUpload"
:after-read="afterRead"
:before-read="beforeRead"
:max-count="1" :preview-full-image="false">
<van-button size="mini" icon="plus" type="primary">上传文件</van-button>
:preview-full-image="false">
<!-- <van-button size="mini" icon="plus" type="primary">上传文件</van-button> -->
<div style="width: 1.6rem;height: 0.6rem;line-height: 0.6rem; background-color: #1989fa;color:#fff;text-align: center;border-radius: 2px;">上传文件</div>
</van-uploader>
</div></van-cell>
</van-cell-group>
......
......@@ -98,38 +98,65 @@ window.addEventListener("load", function() {
}
return true;
},
afterRead(file) { // 调用爱山东中台系统,将数据流传递过去
// gemhoUtil.setCookie('uploading', 'Y')
vant.Toast.loading({
message: '正在处理...',
forbidClick: true,
loadingType: 'spinner',
});
http2.post({
serviceId: API_KEY_MAP["upload"]['id'],
interfacePublicKey: API_KEY_MAP["upload"]["publicKey"],
interfacePrivateKey: API_KEY_MAP["upload"]["privateKey"],
reqParams: {
modelName: 'dangerjob',
},
fileList: [{
fileName: 'file',
file: file.file
}]
}, (res) => {
// gemhoUtil.setCookie('uploading', 'N')
if(res){
console.log('接口回调数据', JSON.parse(res))
var result = JSON.parse(res)
if(result.body && result.body.indexOf('dangerjob') > -1){
this.filePathList = [result.body]
}else{
this.filePathList = []
}
vant.Toast.clear()
afterRead(file) {
console.log('afterRead',file)
// 文件读取完成后的处理逻辑
const formData = new FormData();
formData.append('file', file.file); // 通过FormData构建要上传的文件数据
formData.append('modelName', 'dangerjob');
// 发送HTTP POST请求
fetch(this.previewUrl + '/api/common/upload', {
method: 'POST',
body: formData
})
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error('Network response was not ok.');
})
.then(data => {
console.log(data); // 处理后端返回的数据
// 可根据后端返回的数据做进一步处理,如显示上传成功信息等
this.filePathList = data.body ? [data.body] : []
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
},
// afterRead(file) { // 调用爱山东中台系统,将数据流传递过去
// // gemhoUtil.setCookie('uploading', 'Y')
// vant.Toast.loading({
// message: '正在处理...',
// forbidClick: true,
// loadingType: 'spinner',
// });
// http2.post({
// serviceId: API_KEY_MAP["upload"]['id'],
// interfacePublicKey: API_KEY_MAP["upload"]["publicKey"],
// interfacePrivateKey: API_KEY_MAP["upload"]["privateKey"],
// reqParams: {
// modelName: 'dangerjob',
// },
// fileList: [{
// fileName: 'file',
// file: file.file
// }]
// }, (res) => {
// // gemhoUtil.setCookie('uploading', 'N')
// if(res){
// console.log('接口回调数据', JSON.parse(res))
// var result = JSON.parse(res)
// if(result.body && result.body.indexOf('dangerjob') > -1){
// this.filePathList = [result.body]
// }else{
// this.filePathList = []
// }
// vant.Toast.clear()
// }
// })
// },
resetData() {
// 使用 Object.assign 来重写 this.$data,以达到重置 data 的目的
Object.assign(this.$data, this.$options.data.call(this));
......
......@@ -75,8 +75,11 @@
:before-upload="beforeUpload"
:after-read="afterRead"
:before-read="beforeRead"
:max-count="1" :preview-full-image="false">
<van-button size="mini" icon="plus" type="primary">上传文件</van-button>
:preview-full-image="false">
<!-- <van-button size="mini" icon="plus" type="primary">上传文件</van-button> -->
<div style="width: 1.6rem;height: 0.6rem;line-height: 0.6rem; background-color: #1989fa;color:#fff;text-align: center;border-radius: 2px;">上传文件</div>
</van-uploader>
<!-- <van-uploader v-model="fileList" :max-count="1" :deletable="!disabled" :preview-full-image="false"> -->
</van-uploader>
</div></van-cell>
</van-cell-group>
......
......@@ -27,6 +27,7 @@ window.addEventListener("load", function() {
// companyId: localStorage.getItem('companyId'),
// disabled: false,
fileList: [],
}
},
mounted() {
......@@ -109,39 +110,66 @@ window.addEventListener("load", function() {
}
return true;
},
// afterRead(file) {
// // 获取文件流,调用网关中台接口,获取文件全路径url
// console.log('afterRead',file)
// vant.Toast.loading({
// message: '正在处理...',
// forbidClick: true,
// loadingType: 'spinner',
// });
// http2.post({
// serviceId: API_KEY_MAP["upload"]['id'],
// interfacePublicKey: API_KEY_MAP["upload"]["publicKey"],
// interfacePrivateKey: API_KEY_MAP["upload"]["privateKey"],
// reqParams: {
// modelName: 'emergencydrill',
// },
// fileList: [{
// fileName: 'file',
// file: file.file
// }]
// }, (res) => {
// // gemhoUtil.setCookie('uploading', 'N')
// if(res){
// console.log('接口回调数据', JSON.parse(res))
// var result = JSON.parse(res)
// if(result.body && result.body.indexOf('emergencydrill') > -1){
// this.filePathList = [result.body]
// }else{
// this.filePathList = []
// }
// vant.Toast.clear()
// }
// })
// },
afterRead(file) {
// 获取文件流,调用网关中台接口,获取文件全路径url
console.log('afterRead',file)
// 文件读取完成后的处理逻辑
const formData = new FormData();
formData.append('file', file.file); // 通过FormData构建要上传的文件数据
formData.append('modelName', 'emergencydrill');
vant.Toast.loading({
message: '正在处理...',
forbidClick: true,
loadingType: 'spinner',
});
http2.post({
serviceId: API_KEY_MAP["upload"]['id'],
interfacePublicKey: API_KEY_MAP["upload"]["publicKey"],
interfacePrivateKey: API_KEY_MAP["upload"]["privateKey"],
reqParams: {
modelName: 'emergencydrill',
},
fileList: [{
fileName: 'file',
file: file.file
}]
}, (res) => {
// gemhoUtil.setCookie('uploading', 'N')
if(res){
console.log('接口回调数据', JSON.parse(res))
var result = JSON.parse(res)
if(result.body && result.body.indexOf('emergencydrill') > -1){
this.filePathList = [result.body]
}else{
this.filePathList = []
}
vant.Toast.clear()
// 发送HTTP POST请求
fetch(this.previewUrl + '/api/common/upload', {
method: 'POST',
body: formData
})
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error('Network response was not ok.');
})
.then(data => {
console.log(data); // 处理后端返回的数据
// 可根据后端返回的数据做进一步处理,如显示上传成功信息等
this.filePathList = data.body ? [data.body] : []
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
},
showPopup(popupType) {
if (!this.disabled) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment