我正在具有网络视图的应用程序中实现下载功能。我正在尝试从mpdf输出中下载带有“ D”目标的$mpdf->Output($fileName,'D');
在PHP端,我注意到它总是被两次调用,大概是从浏览器API调用中,并且在downloadListener
上也被downloadManager
调用了enqueue
。
总是这样吗?还是有更好的方法不两次调用api?
我已经实现了这样的downloadListener
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
if (url.startsWith("data:")) {
// TODO: HANDLE DATA AND BLOB URLS
return;
}
Uri sourceUrl = Uri.parse(url);
String cookies = getCookie(sourceUrl.getHost());
// CREATE REQUEST
DownloadManager.Request request = new DownloadManager.Request(sourceUrl);
// INJECT HEADERS
request.addRequestHeader("Cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
// SET FILENBAME
String filename = URLUtil.guessFileName(url, contentDisposition, mimeType);
// SET REQUEST PARAMETERS
request.setTitle(filename);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
// DOWNLOAD
dm.enqueue(request);
}
});