Unhandled Exception: FileSystemException - flutter

When I upload .zip file or .docx ,it deos not work ,but when I choose .c files they work fine
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: Failed to decode data using encoding 'utf-8', path = '/storage/emulated/0/New Text Document.zip'
File _file;
Future upload() async {
if (_file == null) { return; }
String path = _file.path.split("/").last;
var pdf = _file.readAsStringSync();
var url ="http://192.168.1.112/flutter/upload_file.php";
var data = { "path": path, "pdf": pdf };
var response = await http.post(url, body: data);
}
Future pickFile() async {
final myfile = await FilePicker.getFile();
setState(() {
_file = File(myfile.path);
});
}
Here is my php file I tried to use readAsBytesSync and it gave me error
<?php
include 'connection.php';
include 'register.php';
$pdf = $_POST['pdf'];
$file_name = $_POST['path'];
file_put_contents("uploads\\".$file_name, $pdf);
?>

_file.readAsStringSync()
This assumes that your file is plain text. A *.c file most likely is. Neither zip, nor doc nor pdf files are.
What you need to do is read your file as bytes:
var contents = _file.reasAsBytesSync();
Now, I don't know what your API expects to get, so I cannot really help you with how to get the bytes transferred. But this is the way to go.

Related

Firebase hosting file upload via REST with Apps Script

I want to upload a file to Firebase hosting file upload via REST with Apps Script. Been trying to find a solution for days to no avail :( would highly appreciate any recommendations.
I'm following the official documentation here:
https://firebase.google.com/docs/reference/hosting/rest/v1beta1/sites.versions/populateFiles
And I can successfully get the upload URL using this code:
function getUploadURL() {
const YOUR_PROJECT_ID = 'sites/url-shortener-e42ec/versions/dd393a80797d713d';
let postUrl = 'https://firebasehosting.googleapis.com/v1beta1/YOUR_PROJECT_ID:populateFiles';
postUrl = postUrl.replace('YOUR_PROJECT_ID', YOUR_PROJECT_ID);
const options = {
method: 'post',
headers: {
Authorization: `Bearer ${ScriptApp.getOAuthToken()}`,
},
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(postUrl, options);
Logger.log(response);
}
which returns the following:
{
"uploadUrl": "https://upload-firebasehosting.googleapis.com/upload/sites/url-shortener-e42ec/versions/dd393a80797d713d/files"
}
And this is where I get kinda lost because I'm not quite sure on what to do next. The documentation says:
map (key: string, value: string)
A set of file paths to the hashes corresponding to assets that should be added to the version.
A file path to an empty hash will remove the path from the version.
Calculate a hash by Gzipping the file then taking the SHA256 hash of the newly compressed file.
But if I add a payload with a file hash to the call like so:
{
"files": {
"/teste": "3f0749957a1c4d91ed18b8e9df122709974e4e9c94c57f9245794c21dd76d4bd"
}
}
...then I get the error:
{
"error": {
"code": 400,
"message": "Precondition check failed.",
"status": "FAILED_PRECONDITION"
}
}
PART 2 :
The next issue I found is, now that I have the upload URL, I will need to actually upload the file, and according to their documentation I should:
Perform a multipart POST of the Gzipped file contents to the URL using a forward slash and the hash of the file appended to the end.
which I tried with the following apps script code:
function convert(hash) {
return hash.map(byte => ('0' + (byte & 0xFF).toString(16)).slice(-2)).join('');
}
function postFile() {
var files = DriveApp.getFilesByName('abc.txt');
let gzip;
let hash;
if (files.hasNext()) {
var file = files.next();
gzip = Utilities.gzip(file.getBlob());
hash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, gzip.getBytes());
}
let postUrl = 'https://upload-firebasehosting.googleapis.com/upload/sites/url-shortener-e42ec/versions/dd393a80797d713d/files/' + convert(hash);
/*
var textBlob = Utilities.newBlob("abc");
const gzip = Utilities.gzip(textBlob);
const hash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, gzipFile.getBytes());
*/
const data = {
"files": {
"/test.txt": convert(hash)
}
};
const options = {
method: 'post',
headers: {
Authorization: `Bearer ${ScriptApp.getOAuthToken()}`,
accept: 'application/json',
contentType: 'application/json'
},
muteHttpExceptions: true,
payload: JSON.stringify(data)
};
const response = UrlFetchApp.fetch(postUrl, options);
Logger.log(response);
}
... and get the following error:
Couldn't process request (status=412): File url-shortener-e42ec/dd393a80797d713d/0b3b82379e00a1994a46452e8cfd8b2c43ee8599f169a9ee4176253f1a8de469 can't be uploaded.
Appreciate all the help I can get. Thanks in advance!

Unhandled Exception: FileSystemException: Cannot open file, path ... (OS Error: No such file or directory, errno = 2)

I'm trying to download *.xlsx file using dio.download, and it's throwing the errors:
Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Android/data/com.example.foodagator_app/files/file.xlsx' (OS Error: No such file or directory, errno = 2)
Another one error from try/catch block:
FileSystemException: Creation failed, path = 'File: '' (OS Error: Read-only file system, errno = 30)
I wrote the permission in androidmanifest for external storage, and also tried temporary directory, but it's not working. Can anyone help me with this? Here is my code
void download() async {
var tempDir = await getExternalStorageDirectory();
File file = File(tempDir!.path + '/file.xlsx');
try {
Response response = await dio.download(
url,
file,
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
),
);
var raf = file.openSync(mode: FileMode.write);
// response.data is List<int> type
raf.writeFromSync(response.data);
await raf.close();
} catch (e) {
print('Error is: $e');
}
}
void readFile() async {
var tempDir = await getExternalStorageDirectory();
var filePath = tempDir!.path + "/file.xlsx";
var bytes = File(filePath).readAsBytesSync();
var decoder = SpreadsheetDecoder.decodeBytes(bytes, update: true);
for (var table in decoder.tables.keys) {
print(table);
print(decoder.tables[table]!.maxCols);
print(decoder.tables[table]!.maxRows);
for (var row in decoder.tables[table]!.rows) {
print('$row');
}
}
}
This error is getting because there is no file named file.xlsx you can check if file exists or not
if(file.existsSync())
if file does not exist, you can create one using,
new File('$path/file.xlsx').create(recursive: true);
in android 11 and higher use below permission, without tools:ignore="ScopedStorage"
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
In my case, it was caused because I had moved a dart file to another folder but my other files were still referencing that file using the old path, you can import the file again using the new path and using the "package:" keyword to solve this error.
Moral: Don't use relative paths to import a file anywhere in your project, always use the "package:" scheme.
You could make a custom name as well.
String createDownloadDocName(){
return'${fileName}-${DateTime.now().microsecond}';
}
Sorry, I had problem with await usage - I tried to get access to file before I downloaded it

Flutter Downloader: download file from URL with spaces

I'm using Flutter Downloader to download PDF files from an API. On Android I have no issues with the plugin, but on iOS, when the file download URL comes with a space, the file is not downloaded.
final status = await Permission.storage.request();
if (status.isGranted) {
final fileType = widget.billet.uRL.split(".").last;
final rng = Random();
final externalDir = Platform.isIOS
? Platform.isIOS
? await getApplicationDocumentsDirectory()
: await getExternalStorageDirectory()
: await getExternalStorageDirectory();
await FlutterDownloader.enqueue(
url: 'https://${widget.billet.uRL}',
savedDir: externalDir.path,
fileName: '${rng.nextInt(1000).toString()}_boleto_MF7.$fileType',
openFileFromNotification: true,
showNotification: true,
);
} else {
print('No download permission');
}
Example:
File is successfully downloaded: https://company.com/arq/file_123.pdf
File is NOT downloaded successfully: https://company.com/arq/file 123.pdf
When I try to download a file on iOS with the URL with a space, I get the following alert on the console:
"Error retrieving thread information (os/kern) invalid argument"
Is there a way to pass the URL with a space?
It's likely that the URL with spacing wasn't encoded properly which why the download request fails. What you can do here is encode the URL with Uri.encodeFull(url);

Writting and Reading files into mobile devices

I wrote the below code that reads a tring from URL, writting the cotent into a file data.csv then trying to open the file to read its contents as csv but got an error that:
I/flutter ( 6145): FileSystemException: Cannot open file, path =
'data.csv' (OS Error: No such file or directory, errno = 2) I/flutter
( 6145): File is now closed. E/flutter ( 6145):
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception:
FileSystemException: Cannot open file, path = 'data.csv' (OS Error:
Read-only file system, errno = 30)
My code is:
import 'dart:convert';
import 'dart:io';
import 'dart:async';
void _incrementCounter() {
setState(() {
new HttpClient().getUrl(Uri.parse('https://docs.google.com/spreadsheets/d/e/2PACX-1vQvf9tp4-fETDJbC-HRmRKvVFAXEAGO4lrYPpVeiYkB6nqqXdSs3CjX0eBMvjIoEeX9_qU6K2RWmzVk/pub?gid=0&single=true&output=csv'))
.then((HttpClientRequest request) => request.close())
// .then((HttpClientResponse response) => response.transform(new Utf8Decoder()).listen(print));
.then((HttpClientResponse response) => response.pipe(new File('data.csv').openWrite()));
final File file = new File("data.csv");
Stream<List> inputStream = file.openRead();
inputStream
.transform(utf8.decoder) // Decode bytes to UTF-8.
.transform(new LineSplitter()) // Convert stream to individual lines.
.listen((String line) { // Process results.
List row = line.split(','); // split by comma
String city = row[0];
String branches = row[1];
print('$city, $branches');
},
onDone: () { print('File is now closed.'); },
onError: (e) { print(e.toString()); });
// List<List<dynamic>> rowsAsListOfValues = const CsvToListConverter().convert(yourString);
_counter++;
});
}
Your code are not running in the order that you think so you will end up trying to read the file before it has been written. I think you code is going to be easier to understand if you use my answer for a other question you have asked: Reading data from url in List
And again, please read: https://dart.dev/codelabs/async-await

How to delete a file from the local storage in Flutter?

Actually i want to overwrite the file in local storage of phone but it gives me the error
I/flutter ( 3835): /storage/emulated/0/Android/data/com.example.temp/files/flutter_audio_recorder_
I/flutter ( 3835): Exception: A file already exists at the path :/storage/emulated/0/Android/data/com.example.temp/files/flutter_audio_recorder_.wav
so how can i delete this file first ?
i have stored the wav file path in this variable ==> var dirPath ;
Create this method:
Future<void> deleteFile(File file) async {
try {
if (await file.exists()) {
await file.delete();
}
} catch (e) {
// Error in getting access to the file.
}
}
Usage:
deleteFile(File('your_file_path'));