I am creating a quiz app in the Flutter for which I have collected some questions in CSV file. I want to store the CSV file in firebase and display questions into the app by reading from the CSV file. But just to check if the reading file is as simple as it should be, I tried to read a dummy file in this way:
new File('file.txt').readAsString().then((String contents) {
print(contents);
});
from main.dart before returning the Widget.
But i get this error:
`FileSystemException: Cannot open file, path = 'file.txt' (OS Error: No such file or directory, errno = 2)`
even though I have made a dummy 'file.txt' file in the same directory as 'main.dart'.
I tried doing './file.txt' and even the absolute path from windows explorer but none seem to work.
How to fix this?
The path_provider package allows you to access the temp and appDir directory
https://pub.dartlang.org/packages/path_provider
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
You can use the join() method of https://pub.dartlang.org/packages/path to concatenate paths in a platform dependent way, or just use string concatenation like
String filePath = '${appDocDir.path}/file.txt';
new File(filePath).readAsString().then((String contents) {
print(contents);
});
You can use join to safely join files or folders:
Example:
import 'package:path/path.dart' as Path;
String fileName = Path.join('/storage/emulated/0/', 'MyFolder', 'file.txt');
Result:
fileName = '/storage/emulated/0/MyFolder/file.txt'
Related
I want to upload a file using playwright, but as I'm using a wsl, I don't know where I should put the file, for sure not in the windows folder, but where? Cause I tried to put it in the mounted disk in ubunbtu, but it isn't editable, I'm using SetInputFiles().
In the image you can see the file manager that playwright opens, i cant find the node folder in the image.Image
You can keep the file in the project fiolder itself and give relative path for that file. Make one folder in project name it as 'TestData' and copy file in the folder. if file name is test.png then you can give path as give below.
let path = require('path');
let fileToUpload = '/testData/fileUpload/' + fileName;
let absolutePath = path.resolve(process.cwd() + fileToUpload);
await this.page.locator("input[type='file']").setInputFiles(absolutePath);
In flutter it's easy to load a .txt asset at runtime by specifying it or its folder in the pubspec.yaml file and then loading it with rootBundle. However, i'm working on a pure dart package, and I'm struggling to work out how to get the package to load a .txt file relative to it's own directory structure.
When I use the package in a separate dart command line application i'm working on, the relative path that I specified in one of the package source code files causes an error to be thrown that the txt file doesn't exist. I understand why this error is being thrown, because the relative path is interpreted as being from the command line application's root directory instead of the package's root directory, but i'm unsure of how to solve this without specifying the absolute path for the .txt file. I'd rather not specify the absolute path as it makes the package less portable.
Is there anything similar to flutter's asset loading for a pure dart package?
I think you need the resolveSymbolicLinks or resolveSymbolicLinksSync methods to decode the relative path and then use the resolved path to read the txt file:
import 'dart:io';
void main() async {
String file = '../lib/main.dart';
var path = Uri.parse('.').resolveUri(Uri.file(file)).toFilePath();
print(path);
if (path == '') path = '.';
var resolved = await File(path).resolveSymbolicLinks();
print(resolved);
File(resolved).readAsString().then((String contents) {
print(contents);
});
}
I am creating an app in which i will be sending an email with an attached excel sheet.
I already have the excel sheet and my app will be adding some data to the excel sheet before it will be sent.
However after trying to add the excel sheet to my assets folder, adding the path to the pubspec.yaml the File class could not find the file.
pubspec.yaml:
assets:
- logo.png
- Declaratieformulier.xlsx
function:
openFile() {
var bytes = new File("assets/Declaratieformulier.xlsx").readAsBytesSync();
var decoder = new SpreadsheetDecoder.decodeBytes(bytes);
var table = decoder.tables['Blad1'];
I did a little digging and found out assets aren't put on the device as files but are contained within the APK.
So my question is: How do i include an Excel sheet with my app so i can read it and save it as a new Excel file on the phone?
You have to insert the full path in pubspec.yaml so instead of Declaratieformulier.xlsx you have to add assets/Declaratieformulier.xlsx.
assets:
- logo.png
- assets/Declaratieformulier.xlsx
Also you should read the file for the root bundle
import 'package:flutter/services.dart' show ByteData, rootBundle;
...
ByteData data = await rootBundle.load("assets/Declaratieformulier.xlsx");
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
var decoder = SpreadsheetDecoder.decodeBytes(bytes);
You need to read your file with the Flutter rootBundle.
As #LorenzOliveto said, you also need to fix your pubspec.yaml declaration.
ByteData data = await rootBundle.load("assets/Declaratieformulier.xlsx");
// This would be your equivalent bytes variable
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// You can also copy it to the device when the app starts
final directory = await getApplicationDocumentsDirectory();
String filePath = join(directory, "Declaratieformulier.xlsx");
await File(filePath).writeAsBytes(bytes);
How do you read text from a file and write text to a file?
I've been learning about how to read and write text to and from a file. I found another question about reading from assets, but that is not the same. I will add my answer below from what I learned from the documentation.
Setup
Add the following plugin in pubspec.yaml:
dependencies:
path_provider: ^1.6.27
Update the version number to whatever is current.
And import it in your code.
import 'package:path_provider/path_provider.dart';
You also have to import dart:io to use the File class.
import 'dart:io';
Writing to a text file
_write(String text) async {
final Directory directory = await getApplicationDocumentsDirectory();
final File file = File('${directory.path}/my_file.txt');
await file.writeAsString(text);
}
Reading from a text file
Future<String> _read() async {
String text;
try {
final Directory directory = await getApplicationDocumentsDirectory();
final File file = File('${directory.path}/my_file.txt');
text = await file.readAsString();
} catch (e) {
print("Couldn't read file");
}
return text;
}
Notes
You can also get the path string with join(directory.path, 'my_file.txt') but you need to import 'package:path/path.dart'.
Flutter's Official Documentation of Reading and Writing Files
This works for iOS, Android, Linux and MacOS but not for web.
As additional info to #Suragch's answer, if you want to find the file you created, you can do as the images show:
And then inside that data folder, go again to a folder named data and search for your package, and then go to:
If you happen to create new files, in order to be able to see them, just right click and click Synchronize.
An another way to pull the file from the device is by using adb pull command. You can find the file path by debugging the code and then use adb pull command. adb is located in Android SDK -> platform-tools directory.
./adb pull /storage/emulated/0/Android/data/com.innovate.storage.storage_sample/files/sample.txt ~/Downloads
#Suragch 's answer is right. Except the version of path_provider that you want to use now is:
path_provider: ^2.0.9
I'm working with VS2015 and ASP.Net on a webservice application which is installed in the AWS cloud.
In one of my methods i got two files, a PDF and a XML.
These files just exist as instances of type MemoryStream.
Now i have to compress these two "files" in a ZIP file before adding the zip as attachment to an E-mail (class MailMessage).
It seems that i have to save the memorystreams to files before adding them as entries to the zip.
Is ist true or do i have another possibility to add the streams as entries to the zip?
Thanks in advance!
The answer is no.
It is not necessary to save the files before adding them to the stream for the ZIP file.
I have found a solution with the Nuget package DotNetZip.
Here is a code example how to use it.
In that example there two files which only exist in MemoryStream objects, not on a local disc.
It is important to reset the Position property of the streams to zero before adding them to the ZIP stream.
At last i save the ZIP stream as a file in my local folder to control the results.
//DotNetZip from Nuget
//http://shahvaibhav.com/create-zip-file-in-memory-using-dotnetzip/
string zipFileName = System.IO.Path.GetFileNameWithoutExtension(xmlFileName) + ".zip";
var zipMemStream = new MemoryStream();
zipMemStream.Position = 0;
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
textFileStream.Position = 0;
zip.AddEntry(System.IO.Path.GetFileNameWithoutExtension(xmlFileName) + ".txt", textFileStream);
xmlFileStream.Position = 0;
zip.AddEntry(xmlFileName, xmlFileStream);
zip.Save(zipMemStream);
// Try to save the ZIP-Stream as a ZIP file. And suddenly: It works!
var zipFs = new FileStream(zipFileName, FileMode.Create);
zipMemStream.Position = 0;
zipMemStream.CopyTo(zipFs);
zipMemStream.WriteTo(zipFs);
}