dev/flutter

파일 다운로드

wlrn566 2023. 3. 22. 18:37

dio, path_provider 패키지를 설치해준다.

 

flutter pub add dio
flutter pub add path_provider

 

floatingActionButton: FloatingActionButton(
    onPressed: () async {
      var dir = await getTemporaryDirectory();
      log('dir $dir');
    },
    child: const Icon(Icons.download),
  ),

 

디렉토리를 확인해본다.

 

 

디렉토리는 안드로이드 스튜디오의 Device File Explorer 를 사용하면 볼 수 있다.

 

 

 

cache 경로에 이미지가 다운로드 될 것이다.

다운로드할 이미지의 url 을 넣어주고 dio로 다운로드 받는다.

 

floatingActionButton: FloatingActionButton(
        onPressed: () async {

          try {
            var dio = Dio();
            var dir = await getTemporaryDirectory();

            var response = await dio.download(
                'https://cdn.pixabay.com/photo/2015/06/19/21/24/avenue-815297_1280.jpg',
                '${dir.path}/image.jpg',

                onReceiveProgress: (rec, total) {
                  log('rec: $rec, total: $total');
                  // setState(() {
                  //   file = '${dir.path}/image.jpg';
                  // });
                },
            );
            print(response);
          } catch (e) {
            print(e);
          }
        },
        child: const Icon(Icons.download),
      ),

 

다운로드 버튼을 클릭하고 Device File Explorer에서 Synchronize 를 클릭하면 새로고침이 되면서 다운받은 이미지가 나온다.

 

 


 

IOS의 경우 xcode에서 디렉토리에 접근할 수 있다.

xcode에서 Window - Devices and Simulatiors에서 해당 앱의 Container를 다운받아서 패키지보기를 해서 확인할 수 있다.

 

 

 

 


 

+ Directory를 이용해 폴더를 핸들링 할 수 있다.

 

.exists()

디렉토리가 존재하는지 확인할 수 있다.

 

.create()

디렉토리를 생성할 수 있다.

 

.listSync()

디렉토리 내 파일들을 가져올 수 있다.