fluttercommunity / fluttercommunity/flutter_downloader
New Connection Opened when Downloading File
- Dominant language
- Kotlin
- Stars
- 940
- Forks
- 561
- Avg merge
- 1h 18m
- Merged PRs (30d)
- 1
Description
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 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)
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the DownloadWorker entry point shown in the logs and trace how it applies headers, follows the 303 redirect, and handles the login URL. Reproduce the authenticated PDF download with the supplied cookie flow; done means the existing session is preserved and the PDF is downloaded instead of index.php.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, flutter
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100