作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
前期资料:
Language: Flutter/Dart
Flutter Packages: flutter_inappwebview, flutter_downloader
我在使用 Flutter 为 Android 构建的移动应用程序时遇到问题。该应用程序的一项功能是能够下载与 PDF 文件关联的链接。这些文件位于应用程序已经访问的服务器上。用户已通过应用程序登录,因此已建立身份验证。当单击具有 PDF 的 URL 时,它会执行重定向,并返回并下载主页(index.php)。我不知道这是如何发生的,但我有想法。
onDownloadStart: (controller, url) async {
print("onDownloadStart $url");
final taskId = await FlutterDownloader.enqueue(
headers: {'My-Custom-Header': 'custom_value=564hgf34'},
url: url,
savedDir: (await getExternalStorageDirectory()).path,
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);
return taskId;
},
记录:
I/flutter (28552): onDownloadStart https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf
W/WM-WorkSpec(28552): Backoff delay duration less than minimum value
D/DownloadWorker(28552): 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={"My-Custom-Header": "custom_value=564hgf34"},isResume=false
D/DownloadWorker(28552): Update notification: {notificationId: 1, title: https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf, status: 2, progress: 0}
D/DownloadWorker(28552): Open connection to https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf
D/DownloadWorker(28552): Headers = {"My-Custom-Header": "custom_value=564hgf34"}
D/DownloadWorker(28552): Response with redirection code
D/DownloadWorker(28552): Location = https://hjhvtc.online/login/index.php
D/DownloadWorker(28552): New url: https://hjhvtc.online/login/index.php
D/DownloadWorker(28552): Open connection to https://hjhvtc.online/login/index.php
D/DownloadWorker(28552): Headers = {"My-Custom-Header": "custom_value=564hgf34"}
V/InputMethodManager(28552): b/117267690: Failed to get fallback IMM with expected displayId=197 actual IMM#displayId=0 view=com.pichillilorenzo.flutter_inappwebview.InAppWebView.InAppWebView{4592e22 VFEDHVCL. ......ID 0,0-1080,1997}
D/DownloadWorker(28552): Content-Type = text/html; charset=utf-8
D/DownloadWorker(28552): Content-Length = -1
D/DownloadWorker(28552): Charset = UTF-8
D/DownloadWorker(28552): Content-Disposition = null
D/DownloadWorker(28552): fileName = index.php
D/DownloadWorker(28552): Update too frequently!!!!, this should be dropped
D/DownloadWorker(28552): There's no application that can open the file /storage/emulated/0/Android/data/com.coffeepaulconsulting.hjhvtconline/files/index.php
D/DownloadWorker(28552): Update too frequently!!!!, but it is the final update, we should sleep a second to ensure the update call can be processed
D/DownloadWorker(28552): Update notification: {notificationId: 1, title: index.php, status: 3, progress: 100}
D/DownloadWorker(28552): File downloaded
I/WM-WorkerWrapper(28552): Worker result SUCCESS for Work [ id=633dd93d-99e3-46e2-937d-a4782d62a569, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]
最佳答案
经过几次尝试,我终于找到了解决方案。 DownloadWorker 被重定向到登录页面,因为尽管用户在 InAppWebView 中进行了身份验证,但 Flutter 下载器似乎在单独的上下文中运行,因此用户未经过身份验证。
为了维护用户 session ,我创建了一个变量:
// Store cookies to save user session for download
String cookiesString = '';
然后,我创建了
updateCookies
从
CookieManager
检索 cookie 的函数并更新
cookiesString
多变的:
Future<void> updateCookies(Uri url) async {
List<Cookie> cookies = await CookieManager().getCookies(url: url);
cookiesString = '';
for (Cookie cookie in cookies) {
cookiesString += '${cookie.name}=${cookie.value};';
}
print(cookiesString);
}
然后,我调用
updateCookies
在
onLoadStop
InAppWebView 的事件监听器:
onLoadStop: (controller, url) async {
pullToRefreshController.endRefreshing();
if (url != null) {
await updateCookies(url);
}
setState(() {
this.url = url.toString();
urlController.text = this.url;
});
},
最后,我将 cookie 传递给
onDownloadStart
中的 FlutterDownloader 的 header 。事件监听器:
await FlutterDownloader.enqueue(
headers: {
HttpHeaders.authorizationHeader: 'Basic ' +
authToken,
HttpHeaders.connectionHeader: 'keep-alive',
HttpHeaders.cookieHeader: cookiesString,
},
关于android - flutter_inappwebview 和 flutter_downloader - 不知道我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65618744/
我想在我的项目中使用 flutter_downloader 1.1.6,flutter packages get 已完成但我无法构建 apk,它会抛出异常: Launching lib\main.da
我想为我现有的应用程序实现下载功能,但它抛出异常,我不明白为什么。 我新创建了一个新的 flutter 项目并安装了 flutter_downloader 插件,它工作正常,但是当我在现有应用程序中以
前期资料: Language: Flutter/Dart Flutter Packages: flutter_inappwebview, flutter_downloader 我在使用 Flutter
我是一名优秀的程序员,十分优秀!