Commit ebf76af9 authored by xinzhedeai's avatar xinzhedeai

fetch上传图片

parent c52db27e
...@@ -104,8 +104,9 @@ ...@@ -104,8 +104,9 @@
:before-upload="beforeUpload" :before-upload="beforeUpload"
:after-read="afterRead" :after-read="afterRead"
:before-read="beforeRead" :before-read="beforeRead"
:max-count="1" :preview-full-image="false"> :preview-full-image="false">
<van-button size="mini" icon="plus" type="primary">上传文件</van-button> <!-- <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>
</div></van-cell> </div></van-cell>
</van-cell-group> </van-cell-group>
......
...@@ -98,38 +98,65 @@ window.addEventListener("load", function() { ...@@ -98,38 +98,65 @@ window.addEventListener("load", function() {
} }
return true; return true;
}, },
afterRead(file) { // 调用爱山东中台系统,将数据流传递过去 afterRead(file) {
// gemhoUtil.setCookie('uploading', 'Y') console.log('afterRead',file)
vant.Toast.loading({ // 文件读取完成后的处理逻辑
message: '正在处理...', const formData = new FormData();
forbidClick: true, formData.append('file', file.file); // 通过FormData构建要上传的文件数据
loadingType: 'spinner', formData.append('modelName', 'dangerjob');
});
http2.post({ // 发送HTTP POST请求
serviceId: API_KEY_MAP["upload"]['id'], fetch(this.previewUrl + '/api/common/upload', {
interfacePublicKey: API_KEY_MAP["upload"]["publicKey"], method: 'POST',
interfacePrivateKey: API_KEY_MAP["upload"]["privateKey"], body: formData
reqParams: { })
modelName: 'dangerjob', .then(response => {
}, if (response.ok) {
fileList: [{ return response.json();
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() 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() { resetData() {
// 使用 Object.assign 来重写 this.$data,以达到重置 data 的目的 // 使用 Object.assign 来重写 this.$data,以达到重置 data 的目的
Object.assign(this.$data, this.$options.data.call(this)); Object.assign(this.$data, this.$options.data.call(this));
......
...@@ -75,9 +75,12 @@ ...@@ -75,9 +75,12 @@
:before-upload="beforeUpload" :before-upload="beforeUpload"
:after-read="afterRead" :after-read="afterRead"
:before-read="beforeRead" :before-read="beforeRead"
:max-count="1" :preview-full-image="false"> :preview-full-image="false">
<van-button size="mini" icon="plus" type="primary">上传文件</van-button> <!-- <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>
<!-- <van-uploader v-model="fileList" :max-count="1" :deletable="!disabled" :preview-full-image="false"> -->
</van-uploader>
</div></van-cell> </div></van-cell>
</van-cell-group> </van-cell-group>
<van-cell-group :class="{'disable-van-cell': disabled}"> <van-cell-group :class="{'disable-van-cell': disabled}">
......
...@@ -27,6 +27,7 @@ window.addEventListener("load", function() { ...@@ -27,6 +27,7 @@ window.addEventListener("load", function() {
// companyId: localStorage.getItem('companyId'), // companyId: localStorage.getItem('companyId'),
// disabled: false, // disabled: false,
fileList: [], fileList: [],
} }
}, },
mounted() { mounted() {
...@@ -109,39 +110,66 @@ window.addEventListener("load", function() { ...@@ -109,39 +110,66 @@ window.addEventListener("load", function() {
} }
return true; 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) { afterRead(file) {
// 获取文件流,调用网关中台接口,获取文件全路径url
console.log('afterRead',file) console.log('afterRead',file)
// 文件读取完成后的处理逻辑
const formData = new FormData();
formData.append('file', file.file); // 通过FormData构建要上传的文件数据
formData.append('modelName', 'emergencydrill');
vant.Toast.loading({ // 发送HTTP POST请求
message: '正在处理...', fetch(this.previewUrl + '/api/common/upload', {
forbidClick: true, method: 'POST',
loadingType: 'spinner', body: formData
}); })
http2.post({ .then(response => {
serviceId: API_KEY_MAP["upload"]['id'], if (response.ok) {
interfacePublicKey: API_KEY_MAP["upload"]["publicKey"], return response.json();
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() 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) { showPopup(popupType) {
if (!this.disabled) { 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