【问题标题】:PDF not loading from documents folder on App relaunch iOSPDF未从App重新启动iOS上的文档文件夹加载
【发布时间】:2017-09-30 23:53:32
【问题描述】:

我有一个从服务器下载并保存的 pdf。接下来我从UIWebView 中的文件路径打开文件。这在我第一次启动应用程序时有效。当我再次重新启动应用程序时,即使文件路径相同,文档也无法打开。此外,该文档确实存在于应用程序的文档文件夹中。

我正在做类似的事情:-

SaveToFolder.cs

var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), fileName);

using (FileStream destinationStream = File.Create(filePath))
{
     await documentStream.CopyToAsync(destinationStream);
}

第一次保存文件后的文件路径:-

 /var/mobile/Containers/Data/Application/C3EA2325-81CA-4EC9-8C03-479ACF7EE330/Documents/Insufficiency.pdf

应用重新启动时的文件路径

/var/mobile/Containers/Data/Application/C3EA2325-81CA-4EC9-8C03-479ACF7EE330/Documents/Insufficiency.pdf  

是不是我做错了什么?

【问题讨论】:

  • 从 Document 目录加载文件的代码在哪里?更重要的一点文件加载必须在“等待”的异步方法下,因为您正在读取文件并且可能需要时间
  • 嗨@Gagan_iOS,感谢您的回复。我的问题是,在第一次启动时,文件被下载并保存到文件路径,一旦保存,文件就会从文件路径成功打开。但是在第二次启动或关闭应用程序并在一段时间后启动时,相同的路径表示文件不存在。文件在文档目录中的位置

标签: ios pdf xamarin uiwebview nsdocumentdirectory


【解决方案1】:

不久前我遇到了同样的问题。可以参考Can't find saved file (in device) after restarting the app

根据答案

You shouldn't store raw file paths for persistence (or if you do, know that the root can move on you). A better practice would be to only store the relative part of the path and always attach it to the current "root" path in question (particularly if you might be sharing data across devices as with iCloud).

也许你的根也在改变。您可以更改方法并将带有默认路径的文件名附加到您的文档文件夹中,就像在 Xamarin 中一样:-

var docsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); filePath = docsPath +"/" + "Insuffeciency.pdf";

另外,请考虑在保存文件时将您的 Personal 文件夹更改为 MyDocuments 文件夹。

【讨论】:

    【解决方案2】:

    我在 iOS 中创建了一个用于读写文件的文件。请在 iOS 中查看

    using System;
    using Xamarin.Forms;
    using FileReader.iOS;
    using System.IO;
    using FileReader;
    using Foundation;
    using System.Linq;
    using System.Threading.Tasks;
    
    [assembly: Dependency(typeof(SaveAndLoadiOS))]
    
    namespace FileReader.iOS
    {
        public class SaveAndLoadiOS : LoadAndSave
        {
            public static string DocumentPath
            {
                get
                {
                    var documentURL = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User).Last();
                    return documentURL.Path;
                }
            }
    
            public string CreatePath(string fileName)
            {
                return Path.Combine(DocumentPath, fileName);
            }
    
            public async Task SaveTextAsync(string fileName, string text)
            {
                string path = CreatePath(fileName);
                if (IsFileExits(fileName))
                {
                    File.Delete(path);
                }
                using (StreamWriter sw = File.CreateText(path))
                    await sw.WriteAsync(text);  
            }
    
            public async Task<string>  LaodTextAsync(string fileName)
            {
                string path = CreatePath(fileName);
                using (StreamReader sr = File.OpenText(path))
                    return await sr.ReadToEndAsync();
            }
    
            public bool IsFileExits(string fileName)
            {
                return File.Exists (CreatePath(fileName));
            }
        }
    }
    

    为了从我的 .CS 类(ContentPage 的子类)中读取,下面是代码

    var tempFileService =  DependencyService.Get<LoadAndSave>();
    var itemFile = await tempFileService.LaodTextAsync(tempFile.StoredFileName);
    var rootobject = JsonConvert.DeserializeObject<Rootobject>(itemFile);
    

    其中LoadAndSave是如下接口

    using System;
    using System.Threading.Tasks;
    
    namespace FileReader
    {
        public interface LoadAndSave
        {
            Task SaveTextAsync(string fileName, string text);
            Task<string> LaodTextAsync(string fileName);
            bool IsFileExits(string fileName);
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 谢谢。您正在 SaveTextAsync() 中保存文本,如何像 SaveFile() 一样保存 Stream
    • 还有什么是 tempFileServicetempFile.StoredFileName ?我没有使用 Xamarin.Forms 仅供参考,它只是 XAmarin.iOS
    • 我试过你的方法它仍然给我同样的问题。我的问题不在于保存或读取文件。它第一次使用检查文件是否存在的相同代码完美运行。仅在应用程序重新启动时,我检查文件是否存在,即使它存在,它也不会打开。
    • 刚刚更新了我对 tempFileService 的回答。再次为了保存文件,我将我的 JSON 对象(数组字典数组)转换为字符串,然后写入 myfile。
    猜你喜欢
    • 2018-09-18
    • 1970-01-01
    • 2013-09-02
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2015-08-18
    • 2015-10-20
    相关资源
    最近更新 更多