You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin使用Fuel库下载文件:文件名与存储路径设置疑问

Answers to Your Fuel Library Questions

1. How to Specify Download File Name and Storage Directory

Fuel gives you full control over where your downloaded files are saved via the destination closure in the download chain. Here's how to handle it:

Basic Customization

You can directly define the target directory and file name manually to fit your needs:

Fuel.download("https://your-download-url.com/file")
    .destination { response, requestUrl ->
        // Get your app's Documents directory (adjust based on your platform)
        val targetDir = context.getExternalFilesDir(null) ?: context.filesDir
        // Set your preferred custom file name
        val customFileName = "my_downloaded_file.pdf"
        // Return the full target file path
        File(targetDir, customFileName)
    }
    .progress { readBytes, totalBytes ->
        // Keep your existing progress handling logic here
    }
    .response { result ->
        // Process success or failure as needed
    }

Dynamic File Name from Server Headers

If the server sends a suggested file name in the Content-Disposition header, you can parse it dynamically instead of hardcoding:

.destination { response, requestUrl ->
    val targetDir = context.getExternalFilesDir(null) ?: context.filesDir
    // Extract filename from Content-Disposition header (if available)
    val fileName = response.headers["Content-Disposition"]?.let { header ->
        Regex("filename=\"?([^\"]+)\"?").find(header)?.groupValues?.get(1)
    } ?: requestUrl.pathSegments.last() // Fallback to the last segment of the URL
    File(targetDir, fileName)
}

2. Why Can't I Find the temp.tmp File on the Simulator?

That temporary file is part of Fuel's default download workflow—here are the most likely reasons you're not spotting it:

  • Download completed successfully: Fuel creates a temp.tmp file during the download process, but once the download finishes, it renames this file to your final target name. The tmp file no longer exists at that point—check your specified destination directory for the final, renamed file instead.
  • You're looking in the wrong directory: By default, Fuel stores temporary files in your app's cache directory (e.g., /data/data/com.your.package.name/cache/ on Android). To access this on the simulator, use Android Studio's Device File Explorer to navigate to your app's data folder, not external storage or other random directories.
  • Download was interrupted/failed: If the download stopped mid-process, Fuel might automatically clean up the partial temporary file. Test this by intentionally interrupting a download and checking the cache directory right after.
  • Custom destination overrides temp location: If you've specified a custom destination, Fuel might create the temporary file in that same directory (with a .tmp suffix) before renaming it. Double-check your target directory for any hidden or temporary-suffixed files.

内容的提问来源于stack exchange,提问作者Avi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:06:37