微信小程序 - 选择文件并上传(典型实现、结合 Vant Weapp 实现)

一、选择文件并上传,典型实现

wx.chooseMessageFile({
    count: 1,
    type: "file",
    success(res) {
        const tempFilePath = res.tempFiles[0].path;
        console.log("选择的文件路径:" + tempFilePath);

        wx.uploadFile({
            url: "【上传地址】",
            filePath: tempFilePath,
            name: "file",
            success(res) {
                console.log("上传完成", res);
                
                // 更多处理...
            },
            fail(err) {
                console.error("上传失败", err);
            
            	// 更多处理...
            },
        });
    },
});
1、选择文件
wx.chooseMessageFile({
    count: 1,
    type: "file",
    success(res) {
        const tempFilePath = res.tempFiles[0].path;
        console.log("选择的文件路径:" + tempFilePath);

        ...
    },
});
  • 通过 wx.chooseMessageFile API 选择一个文件
  1. count: 1:最多选择 1 个文件

  2. type: "file":选择普通文件(可以是任意类型)

  3. res.tempFiles:一个数组,包含选择的文件信息

  4. res.tempFiles[0].path:文件的临时路径,用于后续上传

2、上传文件
wx.uploadFile({
    url: "【上传地址】",
    filePath: tempFilePath,
    name: "file",
    success(res) {
        console.log("上传完成", res);

        // 更多处理...
    },
    fail(err) {
        console.error("上传失败", err);

        // 更多处理...
    },
});
  • 通过 wx.uploadFile API 将选择的文件上传到指定的地址
  1. url: "【上传地址】":上传文件的服务器地址

  2. filePath:文件的临时路径(从 wx.chooseMessageFile 获取)

  3. name:文件对应的 key,服务器通过该 key 获取文件

  4. success:上传成功时触发,res 包含服务器返回的数据

  5. fail:上传失败时触发,err 包含错误信息


二、选择文件并上传,结合 Vant Weapp 实现

1、引入 Vant Weapp
  • Vant Weapp 官网地址:https://vant-ui.github.io/vant-weapp/#/quickstart
  1. 安装 Vant Weapp:npm i @vant/weapp -S --production

  2. 修改 app.json:将 app.json 中的 "style": "v2" 去除

  3. 点击 【工具】 -> 点击 【构建 npm】

2、具体实现
  • index.json
{
	"usingComponents": {
		"van-uploader": "@vant/weapp/uploader/index"
	}
}
  • index.html
<van-uploader file-list="{{poorFileList}}" max-count="1" bind:after-read="afterReadHandle" />
  • index.js
Page({
	data: {
		myFileList: [],
	},

	afterReadHandle(event) {
		const file = event.detail.file;
		const tempFilePath = file.tempFilePath;
		console.log("选择的文件路径:" + tempFilePath);

		wx.uploadFile({
			url: `【上传地址】`,
			filePath: tempFilePath,
			name: "file",
			success: (res) => {
                console.log("上传完成", res);

				this.setData({
					poorFileList: [
						{
							...file,
							url: 【这里从 res 中取出图片地址】,
						},
					],
				});
                
                // 更多处理...
			},
			fail(err) {
				console.error("上传失败", err);
                
                // 更多处理...
			},
		});
	},
});
C++11引入了对函数指针的增强支持,使得函数指针的声明更加灵活和强大。函数指针是一种特殊的指针型,它存储的是指向函数的地址,可以在运行时调用这些函数。在C++11中,函数指针的声明主要有以下几种形式: 1. **普通函数指针**: ```cpp // 声明指向返回型为T,接受一个参数型的函数 void (*func_ptr)(参数); ``` 例如,声明一个指向整数的加法函数指针: ```cpp int (*add)(int, int); ``` 2. **带默认参数值的函数指针**: ```cpp // 声明指向接受可选参数的函数,如带有默认参数的void函数 void (*func_ptr)(参数型 = 默认值); ``` 注意,这种形式仅限于C++11及更高版本,对于带默认参数的函数,C++11中仍然是按传统的形式声明。 3. **指向成员函数的函数指针**: ```cpp // 声明指向成员函数的函数指针 void (ClassType::*mem_func_ptr)(参数); ``` 用于存储的成员函数地址。 4. **重载函数指针**: ```cpp // C++11之前的方式,使用std::function模板或boost库 // C++11后,使用函数指针模板(如std::function<void(int)>)或直接声明 // 注意:函数指针模板能用于捕获默认参数 std::function<void(参数)> func_ptr; ``` **相关问题--:** 1. C++11如何声明带默认参数值的函数指针? 2. 如何在C++11中声明指向成员函数的函数指针? 3. C++11标准引入了哪些新的函数指针声明语法?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值