【问题标题】:Download manager downloads a bin file if file name not specified (Google Cloud Storage URL)如果未指定文件名,下载管理器会下载 bin 文件(Google Cloud Storage URL)
【发布时间】:2019-02-21 05:48:23
【问题描述】:

我有一个谷歌云存储下载链接,我正在使用 Android DownloadManager 下载文件,当我下载文件时没有指定文件名,它最终会下载一个 .bin 文件。

还有一件事是:如果我尝试从 chrome 浏览器手动下载链接,它工作得非常好。

fun  downloadFile() {
    val uri = Uri.parse("...") //link to the file
    Environment
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
        .mkdirs()

    val request =
        DownloadManager.Request(uri)
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            //.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.jpg")

    val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    downloadID = downloadManager.enqueue(request)
}

【问题讨论】:

标签: android google-cloud-storage android-download-manager


【解决方案1】:

通常使用 HTTP,要下载具有所需文件名的文件,服务器必须将 Content-Disposition 标头设置为类似于 attachment; filename="filename.jpg" 的内容。这可以在 GCS 中使用metadata 进行设置。以这种方式设置 content-disposition 后,DownloadManager 在选择文件名时应该尊重它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 2016-08-19
    • 1970-01-01
    • 2014-01-28
    • 2017-04-03
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多