首页 > 解决方案 > 如何将下载的 PDF 文件保存为从 Rest WS 获得的 byte[] 数组

问题描述

我试图将以前从 REST WS 获得的 PDF 文件保存为 byte[] 数组。

byte[] response = await caller.DownloadFile(url); 
string documentPath = FileSystem.CacheDirectory;
string fileName = "downloadfile.pdf";
string path = Path.Combine(documentPath, fileName);
File.WriteAllBytes(path, response);

我的实际实现没有显示任何错误,但是当我在缓存文件夹中查找文件时,什么都没有,只有一个空文件夹。也尝试将文件放入FileSystem.AppDataDirectoryEnvironment.GetFolderPath(Environment.SpecialFolder.Personal)但任何文件夹中都没有文件

我错过了什么?

提前致谢。

标签: xamarinxamarin.formsxamarin.android

解决方案


string documentPath = FileSystem.CacheDirectory;
string fileName = "downloadfile.pdf";
string path = Path.Combine(documentPath, fileName);

路径会喜欢

"/data/user/0/packagename/cache/downloadfile.pdf"

当你将它保存到内部存储时,你无法看到没有root权限的文件,如果你想查看它,你可以使用 adb工具(Android Debug签名的应用程序)

adb shell
run-as packagename
cd /data/data/packagename
cd cache
ls

然后你可以看到downloadfile.pdf

或者您可以将其保存到外部存储,然后您可以在设备文件管理器中找到它:

//"/storage/emulated/0/Android/data/packagename/files/downloadfile.pdf"
string path = Android.App.Application.Context.GetExternalFilesDir(null).ToString();

推荐阅读