![](/img/trans.png)
[英]Download manager downloads a bin file if file name not specified (Google Cloud Storage URL)
[英]Download Manager Downloads .bin File Only
我在 android 中的下載管理器有問題。 我正在嘗試從 webview 下載 .mp4 視頻文件,但無論我做什么,它總是下載帶有 .bin 擴展名的文件。 它與文件名無關,我知道是因為我嘗試了guessFileName()
函數和手動輸入名稱,但結果是一樣的。 在其他瀏覽器中,下載內容(.mp4)沒有問題。 一個問題可能是關於 mimetype,它返回"application/octet-stream"
因此我嘗試request.setMimeType("video/mp4");
但我仍然保持 mimetype 不變。
在創建:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.setMimeType(mimetype);
request.addRequestHeader("User-Agent", userAgent);
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.setDescription("Downloading File...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
Log.i("TAG1", url);
Log.i("TAG1", contentDisposition);
Log.i("TAG1", mimetype);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
MyWebViewClient 類:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
日志:
I/TAG1: http://someurl.com/someurl
I/TAG1: attachment; filename="filename.mp4"
I/TAG1: application/octet-stream
附加說明:將 URL 重定向到其他 URL 后會發生下載。 但是我嘗試在不重定向 URL 的情況下進行下載,並且仍然得到了 .bin 文件。
刪除
ForceType application/octet-stream
從 .htaccess 解決了這個問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.