uniapp上传图片及后台返回及时渲染

uniapp代码

<view>
	<text style="margin-left: 40rpx;" class="all-tx color-base-text" @click="uploadskm">上传图</text>
	<view>	
		<img @click="uploadskm" style="width:100rpx;margin-left: 40rpx;" src="http://youshuizeling.com/tian/img/tiange.png"/>
		<img @click="" style="width:100rpx;margin-left: 150rpx;" :src= skmimgs  />
	</view>
</view>

先定义下

	data() {
			return {
				skmimgs:'http://youshuizeling.com/tian/img/uploadskm.png'
			};
		},

在methods里面写方法


		methods: {
			
			// 新增代码上传收款码
			uploadskm(){
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: 'https:www.baidu.com', //图片上传接口
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'
							},
							success: (uploadFileRes) => {
								// skmimgs
								var a  = JSON.parse(uploadFileRes.data);
								// console.log(a.data.pic_path);
								// console.log('https://youshuizeling.com/'+ a.data.pic_path);
								this.skmimgs ='https://youshuizeling.com/' + a.data.pic_path;
							}
						});
					}
				});
				
			}
		}

上传

	this.$api.sendRequest({
							url: '/api/memberwithdraw/apply',
							data: {
								apply_money: this.withdrawMoney,
								transfer_type: this.bankAccountInfo.withdraw_type, //转账提现类型
								realname: this.bankAccountInfo.realname,
								mobile: this.bankAccountInfo.mobile,
								bank_name: this.bankAccountInfo.branch_bank_name,
								account_number: this.bankAccountInfo.bank_account,
								applet_type: applet_type,
								skmimg:this.skmimgs
							},
							success: res => {
								if (res.code >= 0) {
									this.$util.showToast({
										title: '成功'
									});
							
								} else {
									if (res.code >= 0) {
										this.$util.showToast({
											title: '失败'
										});
								}
							},
							fail: res => {
								this.isSub = false;
							}
						});

php接受就可以操作
$_REQUEST

  public function shangchuan(){
    
        $base_path = $_SERVER['DOCUMENT_ROOT']."/uploads/h5img/"; // 接收文件目录
        $target_path = $base_path .date("Ymds",time()).$_FILES['file']['name'];
  
        if (move_uploaded_file($_FILES['file']['tmp_name'],$target_path)) {
            
            $filearr = explode("wwwroot/",$target_path);
            $filearr2 = explode("public/",  $filearr[1]);
            $fileinfo = $filearr2[0] .$filearr2[1] ; 
          
            
    
        $array = array (
            "code" => "1",
            "message" =>$fileinfo
        );
        echo json_encode($array);
        
        } else {
            
        $array = array (
            "code" => "0",
            "message" => "There was an error uploading the file, please try again!".$_FILES['file']['error']);
        echo json_encode($array);
        }

    }
posted @ 2022-07-26 10:44  79524795  阅读(643)  评论(0编辑  收藏  举报