Git Product home page Git Product logo

file_saver's Introduction

FileSaver

Discord

Huge Shoutout to all the contributors and the people who are using this package, I'm really grateful to all of you. Thank you for your support.

This plugin package primarily focuses on one task: saving files on Android, iOS, Web, Windows, MacOS, and Linux. It might not have a plethora of features, but it does this job well. This package depends on path_provider for Android and iOS and basic html anchor for Web. The main reason I built this plugin was to avoid using HTML just for downloading files. The plugin is pretty simple and saves the file in Downloads folder in Windows, MacOS, Linux and directly downloads the file in Web, in iOS, the file is Saved in Application Documents Directory, and in Android it is saved in the applications files directory Android/data/your.package.name/file/your_file.extension.

Getting Started

The plugin itself is pretty easy to use. Just call the method saveFile() with respective arguments.

await FileSaver.instance.saveFile({
      required String name,
      Uint8List? bytes,
      File? file,
      String? filePath,
      LinkDetails? link,
      String ext = "",
      MimeType mimeType = MimeType.other,
      String? customMimeType,
      Dio? dioClient,
      Uint8List Function(Uint8List)? transformDioResponse,
      });

This saveFile() method has 8 Named arguments.

String name which takes the name of the file,
Uint8List bytes which will be your actual encoded file,
Or
File file which will be your file in the File object (from dart:io)
Or
Stirng filePath which will be your file path
Or
LinkDetails link which will provide the link, header, request methid and body to your file. LinkDetails can be used as

LinkDetails(
      link: "https://www.example.com/file.extentions",
      headers: {"your-header-key": "you-header-value"},
      method: "POST",
      body: body
)


Out of these parameters, you will have to use atleast one

String ext this will be your file extension.
Another parameter is MimeType type Specifically for Web, which will be your file type

String customMimeType this will be your custom mime type, if you want to use your own mime type, you can use this parameter

Dio dioClient this will be your dio client, if you want to use dio for downloading the file, you can use this parameter

Uint8List Function(Uint8List) transformDioResponse this will be your function to transform the response, if you want to transform the response as per your requirement, you can use this parameter

MimeType is also included in my Package, I've included types for Sheets, Presentation, Word, Plain Text, PDF, MP3, MP4 and many other common formats

or you can call saveAs() only available for android and iOS & macOS at the moment

await FileSaver.instance.saveAs({
      required String name,
      Uint8List? bytes,
      File? file,
      String? filePath,
      LinkDetails? link,
      required String ext,
      required MimeType mimeType,
      String? customMimeType,
      Dio? dioClient,
      Uint8List Function(Uint8List)? transformDioResponse,
      });

All the parameters in this method is same as the saveFile() method.

Note: customMimeType can only be used when mimeType is set to MimeType.custom

Storage Permissions & Network Permissions:

These Settings are optional for iOS, as in iOS the file will be saved in application documents directory but will not be visible in Files application, to make your file visible in iOS Files application, make the changes mentioned below.

iOS:

Go to your project folder, ios/Runner/info.plist and Add these keys:

<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>

iOS

Or in XCode:

Open Your Project in XCode (Open XCode -> Open a project or file -> Your_Project_Folder/ios/Runner.xcworkspace) Open info.plist Add these rows:

Application supports iTunes file sharing (Boolean -> Yes)

Supports opening documents in place (Boolean -> Yes)

iOS Xcode

macOS:

Go to your project folder, macOS/Runner/DebugProfile.entitlements

For release you need to open 'YOUR_PROJECT_NAME'Profile.entitlements

and add the following key:

<key>com.apple.security.files.downloads.read-write</key>
<true/>

MacOS

Or in XCode:

Open Your Project in XCode (Open XCode -> Open a project or file -> Your_Project_Folder/macos/Runner.xcworkspace) Open your entitlement file (DebugProfile.entitlements & 'YOUR_PROJECT_NAME'Profile.entitlements)

Add these rows: MacOS Xcode

and if you get Client Socket Exception while saving files in MacOS from link, you have to add this key in the DebugProfile.entitlements and Release.entitlements of your macOS application and set the value to true

<key>com.apple.security.network.client</key>
<true/>

You can find these files in the project_folder/macos/Runner/ directory.

And You're done

Thank You For Reading this far :)

file_saver's People

Contributors

incrediblezayed avatar milindgoel15 avatar robertodrowaz avatar kush-prof avatar clintonksang avatar bungeefan avatar brutalcoding avatar enzo-santos avatar flowhorn avatar fbernaly avatar keegandc avatar kunwar97 avatar rollingstone avatar silfalion avatar vietstone-ng avatar ksochacki95 avatar clong1995 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.