0

I have a pdf that I download from the server and save it. Next I open the file from the file path within a UIWebView. This works the first time I launch the app. When I relaunch the app again, even thought the file path is the same, the document does not open. Also, the document does exist in the document folder of the app.

I am doing something like :-

SaveToFolder.cs

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

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

File path after saving the document first time :-

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

File Path on app relaunch

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

Is there something Iam doing wrong?

user3034944
  • 1,541
  • 2
  • 17
  • 35
  • Where is the code for loading the File from the Document directory? One more important point File load must be under ASYNC method with 'await' as you are reading file & it may take time – Gagan_iOS May 03 '17 at 08:49
  • Hi @Gagan_iOS, thanks for the reply. My issue is that on the first launch, the file is downloaded and saved to a filepath, once saved the file is successfully opened from the filepath. But on second launch or closing of the app and launching after some time, the same path says file does not exist. where as the file is there in the documents directory – user3034944 May 04 '17 at 04:08

2 Answers2

2

I have created a file in iOS for reading & writing file. Please have a look in 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));
        }
    }
}

For reading from my .CS class (subclass of ContentPage), Below is the code

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

Where LoadAndSave is an interface as below

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);
    }
}

Hope it helps.

Gagan_iOS
  • 3,638
  • 3
  • 32
  • 51
  • Thanks. You are saving text in SaveTextAsync(), How can I save the Stream like my SaveFile() – user3034944 May 04 '17 at 06:30
  • Also what is `tempFileService` and `tempFile.StoredFileName` ? I am not using Xamarin.Forms FYI its just XAmarin.iOS – user3034944 May 04 '17 at 06:34
  • I tried your method it still gives me the same issue. My problem is not with saving or reading the file. It works perfectly the first time with the same code which checks if file exists. Only on app relaunch when I check if file exists even if it does, it does not open. – user3034944 May 04 '17 at 06:55
  • Just updated my answer for tempFileService. Again for saving file, I am converting my JSON object (Array of dictionary of array) into string & then writing into myfile. – Gagan_iOS May 04 '17 at 07:18
1

I ran into the same issue a while ago. You can refer Can't find saved file (in device) after restarting the app

According to the answer

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).

Maybe your root is changing as well. You can change your approach and append the filename with the default path to your documents folder like so in Xamarin:-

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

Also, consider changing your Personal folder to MyDocuments folder while saving the file.

Community
  • 1
  • 1
Pooja Gaonkar
  • 1,546
  • 17
  • 27