我一直试图编辑图像选择器上传的名称,从"image_picker123145468“到我自己的结果,比如"image_for_idblabalbla”。
但到目前为止,我还没有找到任何与此相关的东西。希望这里的人知道,我永远在你的债下哈哈。谢谢!这是我在网上找到的密码。
chooseImage() {
setState(() {
// ignore: deprecated_member_use
file = ImagePicker.pickImage(source: ImageSource.gallery);
});
setStatus('');
}
setStatus(String message) {
setState(() {
status = message;
});
}
startUpload() {
setStatus('Uploading Image...');
if (null == tmpFile) {
setStatus(errMessage);
return;
}
String fileName = tmpFile.path.split('/').last;
upload(fileName); //i think this should be the one that i need to edit but i dont know how.
}
upload(String fileName) {
http.post(uploadEndPoint, body: {
"image": base64Image,
"name": fileName, //i tried manipulating this but no avail
}).then((result) {
setStatus(result.statusCode == 200 ? result.body : errMessage);
}).catchError((error) {
setStatus(error);
});
}发布于 2021-04-07 05:42:11
对代码做了一些修改,并设法更改了上传的名称。这是密码。
startUpload() {
setStatus('Uploading Image...');
if (null == tmpFile) {
setStatus(errMessage);
return;
}
String fileName = tmpFile.path.split("image_picker").last;
String newFilename = fileName.replaceAll(fileName, 'changethistowhatever.jpg);
upload(newFilename);
}发布于 2021-04-01 02:04:24
可以将renameSync与新路径一起使用。
import 'package:path/path.dart' as path;和
file = ImagePicker.pickImage(source: ImageSource.gallery);
String fileDir = path.dirname(file.path);
String newPath = path.join(fileDir, 'newFileName.FileFormat');
file .renameSync(newPath);https://stackoverflow.com/questions/66897268
复制相似问题