본문 바로가기

IT IS IT/Flutter

Flutter Web 에서 Image File 다운로드 하기.

728x90
반응형

 

Flutter에서 파일을 다운로드하기 위해서는 아래 명령어를 많이들 사용한다.

import 'dart:io';
 
그러나 위 기능은 Flutter Web은 지원하지 않는다.
 
Browser-based apps can't use this library 라고 적혀있다.

 

그래서 이를 해결할 수 있는 방법을 찾다보니 image_downloader_web이 존재하는 것을 확인할 수 있었다.

 

해당 라이브러리를 활용하는 방법들이다.

Download image on webb (URL 기반 다운로드)

const _url = "https://picsum.photos/200";

Future<void> _downloadImage() async {
  await WebImageDownloader.downloadImageFromWeb(_url);
}

 

Download image on web from UInt8List (UInt8List 형식 파일 다운로드)

final uint8List = Uint8List();

Future<void> _downloadImage() async {
  await WebImageDownloader.downloadImageFromUInt8List(uInt8List: uint8List);
}

 

 

Download image with reduced quality (저품질 다운로드)

const _url = "https://picsum.photos/200";

Future<void> _downloadImage() async {
  await WebImageDownloader.downloadImageFromWeb(_url, imageQuality: 0.5);
}

Download image with custom headers 

const _url = "https://picsum.photos/200";

Future<void> _downloadImage() async {
  final Map<String, String> headers = {
    'authorization': token,
  };
  await WebImageDownloader.downloadImageFromWeb(_url, headers: headers);
}

나는 image 라이브러리와 연동하여 Flutter 웹 상에서 UInt8List 형식으로 이미지를 편집한 후 다운로드하는 방식으로 활용할 예정이다.

 

728x90
반응형