How can i implement Flutter File Picker in Website - flutter

How can we choose file from local system drives in Flutter web development . Not in App
Please any one suggest me .
I am using like this
I can able to choose file
But how can i get the file path

You need to get the data and create a MediaInfo class for it
class MediaInfo{
String fileName;
String filePath;
}
Get File Path
Future<MediaInfo> convertFileGetNamePath({#required html.File file}) async {
final Map<String, dynamic> infoData = {};
final reader = html.FileReader();
reader.readAsDataUrl(file);
await reader.onLoad.first;
final fileName = file.name;
infoData.addAll({
'name': fileName,
'path': filePath,
});
MediaInfo webImageInfo = MediaInfo();
webImageInfo.fileName = infoData['name'];
webImageInfo.filePath = infoData['path'];
return webImageInfo;
}
Implement it in your code
MediaInfo getInfo = await convertFileGetNamePath(file: imageFile);
fileName = getInfo.fileName;

Related

CRUD folder in Supabase bucket using Flutter

can I CRUD a folder to the Supabase bucket from flutter app using supabase_flutter package?
for example create folder inside 'my_bucket' bucket on Supabase storage and upload a file to it using the code bellow:
final bytes = File('path').readAsBytesSync();
String link = '';
final fileExt = path.split('.').last;
final supabase = Supabase.instance.client;
try {
await supabase.storage
.from('my_bucket') // create folder from here
.uploadBinary(
'file.$fileExt',
bytes,
);
link = await supabase.storage
.from('my_bucket') // get alink for the file
.createSignedUrl('file.$fileExt');
} on PostgrestException catch (e) {
print('ErrorSupa:${e.message}');
}
You can just pass the path to the file like the following, and necessary folders will be created automatically.
final bytes = File('path').readAsBytesSync();
String link = '';
final fileExt = path.split('.').last;
final supabase = Supabase.instance.client;
await supabase.storage
.from('my_bucket') // create folder from here
.uploadBinary(
'path/to/file/file.$fileExt',
bytes,
);
link = await supabase.storage
.from('my_bucket') // get alink for the file
.createSignedUrl('path/to/file/file.$fileExt');

how to give title in flutter to pdf file before save to download folder?

I want exactly this page in flutter
I'm not sure where that screen is coming from. But you can try the file_picker_writable plugin. (shameless plug).
Future<void> _openFilePickerForCreate() async {
final rand = Random().nextInt(10000000);
final fileInfo = await FilePickerWritable().openFileForCreate(
fileName: 'newfile.$rand.codeux',
writer: (file) async {
final content = 'File created at ${DateTime.now()}\n\n';
await file.writeAsString(content);
},
);
if (fileInfo == null) {
_logger.info('User canceled.');
return;
}
final data = await _appDataBloc.store.load();
await _appDataBloc.store
.save(data.copyWith(files: data.files + [fileInfo]));
}
}
this will open a folder chooser dialog for the user and a default file name (given by fileName).
This code example will open the following dialog:

Is there a way to download folder from firebase storage with flutter?

I want to download .epub files from firebase storage. I can download image file cause I know imageUrl but not .epub file url. How should I do? I store fileName, imageUrl in Firestore but I don't know epub file's url . So I can't store it.
downloadFile(fileName,imageUrl) async{
Dio dio=Dio();
final storageRef=FirebaseStorage.instance.ref();
final imageUrls =await storageRef.child("Featured").child('a clock orange/Anthony-Burgess-A-Clockwork-Orange-W.-W.-Norton-_-Company-_1986_.epub').getDownloadURL();
String savePath= await getPath(fileName);
dio.download(imageUrls, savePath,
onReceiveProgress: (rcv,total){
setState((){
progress=((rcv/total) *100).toStringAsFixed(0);
});
if (progress == '100') {
setState(() {
isDownloaded = true;
});
}
}).then((_){
if (progress=="100"){
setState(() {
isDownloaded=true;
});
}
});}
I tried this. But it didn't work.
.
Use Firebase's writeToFile instead of dio's download.
final fileRef = storageRef.child("<path here>");
final appDocDir = await getApplicationDocumentsDirectory();
final filePath = "${appDocDir.absolute}/<path here>";
final file = File(filePath);
final downloadTask = fileRef.writeToFile(file);
downloadTask.snapshotEvents.listen((taskSnapshot) {
...
}
See Download to a local file for details.

How to merge two audio files with just_audio for playing both at the same time?

How to merge (not concat) two audio files with just_audio for playing both at the same time?
I want to merge it to avoid a hardware limit of just_audio instances playing at the same time. To play with just one instance of just_audio.
The only way I have found to concatenate multiple audio files is to use ffmpeg.
Add this to your pub.dev flutter_ffmpeg and add this class to your lib folder:
class FFmpeg {
static Future<File> concatenate(List<String> assetPaths, {String output = "new.mp3"})async{
final directory = await getTemporaryDirectory();
final file = File("${directory.path}/$output");
final ffm = FlutterFFmpeg();
final cmd = ["-y"];
for(var path in assetPaths){
final tmp = await copyToTemp(path);
cmd.add("-i");
cmd.add(tmp.path);
}
cmd.addAll([
"-filter_complex",
"[0:a] [1:a] concat=n=${assetPaths.length}:v=0:a=1 [a]",
"-map", "[a]", "-c:a", "mp3", file.path
]);
await ffm.executeWithArguments(cmd);
return file;
}
static Future<File>copyToTemp(String path)async{
Directory tempDir = await getTemporaryDirectory();
final tempFile = File('${tempDir.path}/${path.split("/").last}');
if(await tempFile.exists()){
return tempFile;
}
final bd = await rootBundle.load(path);
await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
return tempFile;
}
}
Example:
final track = await FFmpeg.concatenate(
[
"assets/audios/test1.mp3",
"assets/audios/test2.mp3",
"assets/audios/test3.mp3",
],
output: "output.mp3"
);

Make PlatformFile into File in Flutter using File Picker

I am using the File Picker Plugin to choose a file from a device. The file is chosen in the datatype of a PlatformFile, but I want to send the file to Firebase Storage and I need a regular File for that. How can I convert the PlatformFile into a File so that I can send it to Firebase Storage? Here is the code:
PlatformFile pdf;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
void _trySubmit() async {
final isValid = _formKey.currentState.validate();
if (isValid) {
_formKey.currentState.save();
final ref = FirebaseStorage.instance
.ref()
.child('article_pdf')
.child(title + '-' + author + '.pdf');
await ref.putFile(pdf).onComplete; // This throws an error saying that The argument type 'PlatformFile' can't be assigned to the parameter type 'File'
}
}
void _pickFile() async {
FilePickerResult result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['pdf'],
);
if (result != null) {
pdf = result.files.first;
}
}
Try this:
PlatformFile pdf;
final File fileForFirebase = File(pdf.path);
Happy coding! :)
If you're on a web app, you can post image files to Firestore with flutter_file_picker: (Taken from the FAQ page): https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ
// get file
final result = await FilePicker.platform.pickFiles(type: FileType.any, allowMultiple:
false);
if (result.files.first != null){
var fileBytes = result.files.first.bytes;
var fileName = result.files.first.name;
// upload file
await FirebaseStorage.instance.ref('uploads/$fileName').putData(fileBytes);
}
This works
File(platformFile.name)
Just be sure not duplicates in the file names in your logic.