var filePaths = that.imgList; console.log(filePaths) // 首先判断文件是否存在 uni.getFileInfo({ filePaths, success: () => { console.log('文件存在'); uni.removeSavedFile({ filePaths, success: () => { console.log('成功删除本地缓存中的文件'); // 同时从 imgList 数组中删除对应的元素 that.imgList = []; that.saveData = []; const taskData = JSON.stringify(this.fromData); uni.removeStorageSync(this.fromData.number, taskData); }, fail: (err) => { console.log('删除本地缓存中的文件失败', err); }, }); }, fail: (err) => { console.log('文件不存在', err); // 如果文件不存在,则直接从 imgList 数组中删除对应的元素 that.imgList = []; that.saveData = []; }, });这是uniapp如何把filePaths循环删除这是一个数组
时间: 2024-03-21 18:39:34 浏览: 14
这段代码中并没有涉及到循环删除数组中的元素,而是通过判断文件是否存在,然后删除对应的本地缓存文件以及从数组中删除对应的元素。如果要循环删除数组中的元素,可以使用JavaScript中的forEach()方法或者for循环来实现。例如:
```
let arr = [1, 2, 3, 4, 5];
arr.forEach((item, index) => {
if (item % 2 === 0) {
arr.splice(index, 1);
}
});
console.log(arr); // [1, 3, 5]
```
这里使用forEach()方法遍历数组,如果数组元素为偶数,则使用splice()方法从数组中删除对应的元素。最后输出删除后的数组。
相关问题
onChooseAvatar1() { let that = this wx.chooseMessageFile ({ count: 1, success(res) { let tempFiles = res.tempFiles; let paths = tempFiles.map(file => file.path); // 提取路径 if (this.data.imgList.length != 0) { this.setData({ imgList: this.data.imgList.concat(paths) // 添加路径 }) } else { this.setData({ imgList: paths // 添加路径 }) } wx.uploadFile({ url: app.d.laikeUrl+'&action=user&m=upload', filePath: that.data.avatarUrl1, name: 'file', success(res) { that.data.navatarUrl1 = res.data; console.log('上传成功:', res.data); } }) }, fail(res){ console.log("选择失败",res) } }) },如何修正错误
在这段代码中,您使用了 `wx.chooseMessageFile()` 方法来选择上传的文件,并在成功回调中执行了上传操作。然而,您在上传操作中使用了 `that.data.avatarUrl1` 作为文件路径,而实际上应该使用 `paths` 变量来代替,因为它是您从选择文件成功回调中获取到的文件路径。
因此,您需要将 `wx.uploadFile()` 方法中的 `filePath` 属性改为 `paths`,即:
```
wx.uploadFile({
url: app.d.laikeUrl+'&action=user&m=upload',
filePath: paths,
name: 'file',
success(res) {
that.data.navatarUrl1 = res.data;
console.log('上传成功:', res.data);
}
})
```
这样就可以上传正确的文件了。同时,您还需要注意的是,在选择文件成功回调中,应该将 `this` 关键字改为 `that`,以避免在回调函数中出现 `this` 上下文丢失的问题。
uni.request({ url: config.baseUrl + '/API/Task/getToken', method: 'GET', success(res) { that.baseUrl = res.data.domain that.token = res.data.token; // 获取到 token 后再进行上传 that.imgList.push(e.tempFilePaths[0]); const fileName = e.tempFilePaths[0].substr(e.tempFilePaths[0] .lastIndexOf('/') + 1); // 获取文件名 const fileExt = fileName.substr(fileName.lastIndexOf('.') + 1) .toLowerCase(); // 获取文件拓展名,并转换为小写 const today = new Date().toISOString().slice(0, 10); // 获取当天日期,格式为 yyyy-mm-dd const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate(); const hour = now.getHours(); const minute = now.getMinutes(); const currentDateTime = ${year}-${month}-${day}-${hour}:${minute}:00; var nums = []; that.uploadCount++; // 上传次数加一 const newFileName = currentDateTime + '' + that.uploadCount + '.' + fileExt; // 生成新的文件名,以上传次数为前缀 const path = that.fromData.c_name + '/' + that.fromData.a_name + '/' + that.fromData.m_name + '/' + that.fromData.mold + '/' + that.fromData.m_title + '/' + that.fromData.r_name + '-' + that.uploadCount + '/' + that.fromData.o_name + '/' + today; const key = path + '/' + newFileName that.imgData.push(key) uni.uploadFile({ url: 'https://upload-z1.qiniup.com', filePath: e.tempFilePaths[0], name: 'file', formData: { token: res.data.token, key: path + '/' + newFileName // 将路径和文件名拼接起来 }, success(res) { const data = JSON.parse(res.data); const key = data.key const parts = key.split("."); const KeynewName = parts.slice(0, -1).join(".") + ".jpg"; that.baseList.push(that.baseUrl + KeynewName) that.Keyname.push(KeynewName) for (var i = 0; i < that.Keyname.length; i++) { var num = that.Keyname[i].split('/').slice(-1)[0].split('.')[0].split('')[1]; nums.push(num); } console.log('存在的文件名:'+nums) }, fail(erro) { console.log(erro); } }); }, fail(err) { console.log("获取 Token 失败:", err); } });我想让这里的uploadCount先判断nums中存在哪些,uploadCount只能为1,2,3 如果nums存在1,2那么uploadCount只能为3
可以在上传之前先判断 nums 数组中是否存在 1 和 2,并将结果保存在两个变量中,比如 hasOne 和 hasTwo。然后根据这两个变量来判断 uploadCount 的值,如果 hasOne 和 hasTwo 都为 true,那么 uploadCount 只能为 3,否则 uploadCount 可以为 1、2 或 3。可以参考下面的代码:
```
// 判断 nums 数组中是否存在 1 和 2
var hasOne = nums.includes('1');
var hasTwo = nums.includes('2');
// 根据 hasOne 和 hasTwo 来判断 uploadCount 的值
if (hasOne && hasTwo) {
that.uploadCount = 3;
} else {
that.uploadCount = Math.floor(Math.random() * 2) + 1;
}
```
这段代码中,使用了 includes 方法来判断 nums 数组中是否存在 1 和 2,如果存在则将 hasOne 和 hasTwo 设置为 true,否则为 false。然后根据 hasOne 和 hasTwo 的值来判断 uploadCount 的值,如果都为 true,则 uploadCount 只能为 3,否则可以随机生成 1 或 2。