Flutter WEB download option

前端 未结 6 1792
萌比男神i
萌比男神i 2021-01-01 15:43

I am making flutter web app that should generate a file from user data. And have the option to download the output file.

But I can not find any options/packages whic

6条回答
  •  再見小時候
    2021-01-01 16:00

    Response to bounty:

    the case of an audio file it just starts playing rather than downloading it

    I've done same thing with video, but I'm sure it will work with audio as well. I assume, your generated audio is an array or blob

        import 'dart:js' as JS;
        import 'dart:html' as HTML;
    
        const mimeType = 'audio/wav'; // TODO: Pick a right format
    
        void downloadFile(List chunks) async {
            final blob = HTML.Blob(chunks, mimeType);
            final objectUrl = await HTML.Url.createObjectUrlFromBlob(blob);
            final a = HTML.AnchorElement();
            a.href = item.url;
            a.download = "my_audio_file_${DateTime.now()}.wav";
            HTML.document.body.append(a);
            a.click();
            // Wait for click event to be processed and cleanup
            await Future.delayed(Duration(milliseconds: 100));
            a.remove();
            HTML.Url.revokeObjectUrl(item.videoObjectUrl);
        }
    

提交回复
热议问题