|
[YonStudio]
没办法将照片上传到服务器上
[复制链接]
为什么这段代码没办法传图片到服务器端口,纯新手o(╥﹏╥)o
<template>
<scroll-view class="main" scroll-y>
<camera id="camera" v-if="scanCodeMode" mode="scanCode" onstop={this.onstop} onerror={this.onerror}
oninitdone={this.oninitdone}>
</camera>
<view v-else>
<camera id="camera" device-position={this.data.devicePosition} onstop={this.onstop} onerror={this.onerror}
oninitdone={this.oninitdone}>
<scroll-view class="btns" scroll-x>
<button onclick={this.takePhoto}>拍照</button>
<button @click="uploadPhoto">上传照片</button>
</scroll-view>
</camera>
<text>图片预览:</text>
<image id="image" src={this.previewUrl} mode="widthFix" />
</view>
</scroll-view>
</template>
<style>
.main {
width: 100%;
height: 100%;
}
#camera {
width: 100%;
height: 400px;
background: #ccc;
}
.btns {
align-items: center;
width: 100%;
height: 44px;
position: absolute;
bottom: 0;
background: rgba(50, 50, 50, 0.3);
}
#image {
width: 100%;
}
</style>
<script>
export default {
name: 'test',
data() {
return {
devicePosition: 'back',
previewUrl: '',
videoThumbPath: '',
videoPath: '',
photoPath: '',
scanCodeMode: false
}
},
methods: {
onstop(e) {
console.log('onstop');
},
onerror(e) {
console.log('onerror');
},
oninitdone(e) {
console.log('oninitdone');
},
takePhoto(e) {
var that = this;
var obj = document.getElementById('camera');
obj.takePhoto({
quality: 'high'
}, function (ret, err) {
that.photoPath = ret.tempImagePath; // 修改赋值为 photoPath
});
},
uploadPhoto() {
var that = this;
var obj = document.getElementById('camera');
obj.takePhoto({
quality: 'high'
}, function (ret, err) {
var photoPath = ret.tempImagePath;
that.uploadFile(photoPath);
});
},
uploadFile(photoPath) {
var that = this;
var photoPath = that.photoPath; // 使用 photoPath 属性
var files = {
photo: photoPath // 键为 "photo",值为照片路径
};
api.ajax({
url: 'http://192.168.1.152:8080/predict',
method: 'post',
data: {
values: {
name: 'haha'
},
files: files // 将文件字段及其对应的路径传递给 "files"
}
}, function (ret, err) {
if (ret) {
api.alert({ msg: JSON.stringify(ret) });
} else {
api.alert({ msg: JSON.stringify(err) });
}
});
},
}
}
</script> |
|