如果未指定文件名,下载管理器会下载bin文件(Google云端存储URL)

问题描述 投票:0回答:1

我有一个谷歌云存储下载链接,我使用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个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.