学习bug

2025.8.6,在发帖功能中,一直出现上传的图片页面无法上传的问题,bucket中有上传的图片,但是数据库中的image_url一直为空,复制up的源码也没有用

​记录一下我的解决办法

​export const uploadFile = async (image_key: string, file: ImageResult) => {

try {

const res = await storage.createFile('689318ae002bdaf1ece1', image_key, {

name: image_key,

type: 'image/jpeg',

size: file.height * file.width,

uri: file.uri,

});

const fileId = res.$id;

// 手动拼接图片 URL

const endpoint = 'https://cloud.appwrite.io/v1';

const projectId = 'XXX';

const bucketId = 'XXX';

const fileUrl = `{endpoint}/storage/buckets/{bucketId}/files/{fileId}/view?project={projectId}&mode=admin`;

return {

fileId,

fileUrl,

};

} catch (error) {

console.log('uploadFile error:', error);

throw error;

}

};

​const pickImage = async () => {

try {

let result = await ImagePicker.launchImageLibraryAsync({

mediaTypes: "images", "videos",

allowsEditing: true,

aspect: 4, 3,

quality: 1,

});

if (!result.canceled) {

const compressedImage = await compressImage(result.assets0.uri);

if (compressedImage) {

if (!compressedImage) {

Alert.alert("图片压缩失败");

return;

}

const { fileId, fileUrl } = await uploadFile(

ID.unique(),

compressedImage

);

if (!fileUrl) {

Alert.alert("图片上传失败");

return;

}

setImage(fileUrl);

}

}

} catch (error) {

console.log(error);

Alert.alert("图片选择失败");

}

};

​希望能帮助后来的朋友😘👌

相关推荐
2601_962071576 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
香芋芋圆6 小时前
AI 冲击内卷之下,普通前端如何破局?WebGIS—— 低门槛突围赛道
前端·javascript·人工智能·学习·职场发展
INS_KF7 小时前
【编程笔记】成员函数中两个 const 的区别(const Data &getData() const;)
前端·javascript·笔记
淡海水7 小时前
07-04-并发-ConcurrentBag-T-工作窃取WorkStealing算法
开发语言·算法·c#·bag·concurrent·workstealing
宿6747 小时前
vue3-async
前端·javascript·vue.js
CS_Zero7 小时前
C语言sizeof是函数吗?
c语言·开发语言
-今昭-8 小时前
《V2V 迁移实战:将 VMware 虚拟机转为 OpenStack 可用 Glance qcow2 镜像》
开发语言·python
excel8 小时前
Nuxt + Twin CSS 中宽度超过屏幕时底部出现空白的原因与解决方案
前端·javascript
黑马程序员毕设10 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
_Twink1e11 小时前
openJiuwen 实训营 Day 1 · WorkSwarm 入门教程
开发语言·c++·openjiuwen