Flutter应用向Supabase Storage上传图片得到空白损坏文件问题求助
问题根因
代码中执行了file.writeAsStringSync('File content')操作,将原始图片文件的全部内容强制覆盖为文本字符串File content。该字符串的UTF-8编码长度恰好为12字节,和你观测到的上传后文件大小完全匹配,因此你实际上传的是仅包含这段无效文本的空文件,并非选中的原始图片。
修复方案
直接删除这行多余的文件写入逻辑即可,修正后的完整上传代码如下:
File file = File(image.path); print('file $file'); // 上传文件到public桶 final storageResponse = await supabase.storage.from('public').upload(image.name, file); print('upload response : ${storageResponse.data}'); // 获取下载地址 final urlResponse = await supabase.storage.from('public').createSignedUrl(image.name, 60); print('download url : ${urlResponse.data}');
可选优化建议
- 上传前可添加文件大小校验,避免异常空文件上传:
print('原始文件大小:${file.lengthSync()}字节'); - 如果需要覆盖同名文件,可在upload方法中添加upsert参数:
await supabase.storage.from('public').upload( image.name, file, fileOptions: const FileOptions(upsert: true), );
内容的提问来源于stack exchange,提问作者Jake Anderson
相关产品推荐
相关产品推荐

