flutter_downloader icon indicating copy to clipboard operation
flutter_downloader copied to clipboard

New Connection Opened when Downloading File

Open CelestialReaver opened this issue 4 years ago • 6 comments

Hello - I'm honestly not sure if this is an issue or by design. I'm running into an issue with accessing a file that requires authentication first. I'm using flutter_inappwebview with no issues moving around the site after logging in. However, when I click on a pdf to download flutter_downloader states a 303 was received and downloads an index.php file. So here's my question:

Is it possible to pass the current cookie session to flutter_downloader so it uses the existing session to download instead of opening a new session?

Below is the message I get when trying to download the PDF. To reiterate, I am logged into the app and can navigate with no issues. I can close the app and open it back up without needing to log in again.

I/flutter ( 2824): Cookie Name: MoodleSession
I/flutter ( 2824): Cookie Value: q4j8i7ko967g4pbfa0at749o2d
I/flutter ( 2824): [{name: MoodleSession, value: q4j8i7ko967g4pbfa0at749o2d, expiresDate: null, isSessionOnly: null, domain: null, sameSite: null, isSecure: null, isHttpOnly: null, path: null}, {name: MOODLEID1_, value: %250D%25ED2_, expiresDate: null, isSessionOnly: null, domain: null, sameSite: null, isSecure: null, isHttpOnly: null, path: null}]
I/flutter ( 2824): onDownloadStart https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf
W/WM-WorkSpec( 2824): Backoff delay duration less than minimum value
D/DownloadWorker( 2824): DownloadWorker{url=https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf,filename=null,savedDir=/storage/emulated/0/Android/data/com.coffeepaulconsulting.hjhvtconline/files,header={"MoodleSession": "q4j8i7ko967g4pbfa0at749o2d"},isResume=false
D/DownloadWorker( 2824): Update notification: {notificationId: 5, title: https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf, status: 2, progress: 0}
D/DownloadWorker( 2824): Open connection to https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf
D/DownloadWorker( 2824): Headers = {"MoodleSession": "q4j8i7ko967g4pbfa0at749o2d"}
D/DownloadWorker( 2824): Response with redirection code
D/DownloadWorker( 2824): Location = https://hjhvtc.online/login/index.php
D/DownloadWorker( 2824): New url: https://hjhvtc.online/login/index.php
D/DownloadWorker( 2824): Open connection to https://hjhvtc.online/login/index.php
D/DownloadWorker( 2824): Headers = {"MoodleSession": "q4j8i7ko967g4pbfa0at749o2d"}
D/DownloadWorker( 2824): Content-Type = text/html; charset=utf-8
D/DownloadWorker( 2824): Content-Length = -1
D/DownloadWorker( 2824): Charset = UTF-8
D/DownloadWorker( 2824): Content-Disposition = null
D/DownloadWorker( 2824): fileName = index.php
D/DownloadWorker( 2824): Update too frequently!!!!, this should be dropped
D/DownloadWorker( 2824): Setting an intent to open the file /storage/emulated/0/Android/data/com.coffeepaulconsulting.hjhvtconline/files/index.php
D/DownloadWorker( 2824): Update too frequently!!!!, but it is the final update, we should sleep a second to ensure the update call can be processed
D/DownloadWorker( 2824): Update notification: {notificationId: 5, title: index.php, status: 3, progress: 100}
D/DownloadWorker( 2824): File downloaded
I/WM-WorkerWrapper( 2824): Worker result SUCCESS for Work [ id=bdf338a7-9325-47a6-9b50-d8765b62204f, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]

Below is the code I'm using:

onDownloadStart: (controller, url) async {
                  List<Cookie> cookies = await _cookieManager.getCookies(url: url.toString());
                  final cookieName = cookies.toString().split("value:").first.split(":").last.split(",").first.toString().trim();
                  final cookieValue = cookies.toString().split("expiresDate:").first.split("value:").last.split(",").first.toString().trim();
                  print("Cookie Name: $cookieName");
                  print("Cookie Value: $cookieValue");
                  print(cookies);
                  print("onDownloadStart $url");
                  final taskId = await FlutterDownloader.enqueue(
                    url: url,
                    savedDir: (await getExternalStorageDirectory()).path,
                    headers: { cookieName : cookieValue},
                    showNotification: true,
                    openFileFromNotification: true, // click on notification to open downloaded file (for Android)
                  );
                 },

I'm happy to answer any questions or provide more information. Thank you!

** Additional Info **

flutter doctor -v
[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Version 10.0.19042.685], locale en-US)
    • Flutter version 1.22.5 at C:\src\flutter
    • Framework revision 7891006299 (5 weeks ago), 2020-12-10 11:54:40 -0800
    • Engine revision ae90085a84
    • Dart version 2.10.4

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\pvang\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.3
    • ANDROID_SDK_ROOT = C:\Users\pvang\AppData\Local\Android\sdk
    • Java binary at: C:\Users\pvang\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] VS Code, 64-bit edition (version 1.52.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.18.1

[√] Connected device (1 available)
    • Pixel 5 (mobile) • 09031FDD40046F • android-arm64 • Android 11 (API 30)

CelestialReaver avatar Jan 17 '21 00:01 CelestialReaver

I have the same problem

dpatrongomez avatar Mar 28 '21 22:03 dpatrongomez

The same problem here, any news?

sgonzalez9450 avatar Mar 31 '21 08:03 sgonzalez9450

same issue is facing

Ajay55 avatar May 01 '22 04:05 Ajay55

I have same problem, guys. Has anyone been able to find a solution?

JesterVZ avatar May 18 '22 08:05 JesterVZ

hey all - sorry for the late reply - this is what worked for me:

                  onDownloadStart: (controller, url) async {
                    var appPath = await getExternalStorageDirectory();
                    List<Cookie> cookies =
                        await _cookieManager.getCookies(url: url.toString());
                    final taskId = await FlutterDownloader.enqueue(
                      url: url,
                      savedDir: appPath.path,
                      headers: {
                        "Cookie":
                            "${cookies[0].name}=${cookies[0].value}; ${cookies[1].name}=${cookies[1].value}"
                      },
                      showNotification: true,
                      openFileFromNotification:
                          true, // click on notification to open downloaded file (for Android)
                    );
                  }

i hope this helps!

CelestialReaver avatar Jun 06 '22 14:06 CelestialReaver

Hi, I'm still having the same problem as before. When I download something in the console appears Content-Type = text/html when I have indicated application/pdf

dpatrongomez avatar Jun 06 '22 17:06 dpatrongomez

_cookieManager

Undefined name '_cookieManager'. Try correcting the name to one that is defined, or defining the name.

asrafulattare avatar Dec 02 '22 15:12 asrafulattare