2017-05-03 18 views
0

서버에서 다운로드하여 저장하는 pdf 파일이 있습니다. 다음으로 나는 UIWebView 내의 파일 경로에서 파일을 엽니 다. 이것은 앱을 처음 실행할 때 작동합니다. 응용 프로그램을 다시 실행해도 파일 경로가 같다고 생각하더라도 문서가 열리지 않습니다. 또한 문서는 앱의 문서 폴더에 있습니다.응용 프로그램의 문서 폴더에서 PDF가 로딩되지 않습니다. iOS

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

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

파일 경로 문서를 처음 저장 한 후

SaveToFolder.cs : - - :

내가 좋아하는 일을하고 응용 프로그램의 재발에

/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 

잘못된 일이 있습니까?

+0

문서 디렉토리에서 파일을로드하는 코드는 어디에 있습니까? 한 가지 더 중요한 포인트 파일로드는 파일을 읽는 중 '대기 중'인 ASYNC 방식이어야하며 시간이 걸릴 수 있습니다. –

+0

안녕하세요 @Gagan_iOS, 답장을 보내 주셔서 감사합니다. 내 문제는 첫 번째 실행에서 파일이 다운로드되고 파일 경로에 저장되면 저장 한 파일이 파일 경로에서 성공적으로 열린다는 것입니다. 그러나 앱을 두 번째 실행하거나 닫은 후 얼마 후에 실행되는 동일한 경로는 파일이 존재하지 않는다고 말합니다. 파일이 문서 디렉토리에있는 곳은 – user3034944

답변

1

이전에 동일한 문제가 발생했습니다. 당신은 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).

어쩌면 루트뿐만 아니라 변화 대답에 따르면 Can't find saved file (in device) after restarting the app

참조 할 수 있습니다. 당신은 당신의 접근 방식을 변경하고 문서 자 마린 년과 같이 폴더의 기본 경로와 파일 이름을 추가 할 수 있습니다 : - 또한

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

는 파일을 저장하는 동안 MyDocuments 폴더로 Personal 폴더를 변경하는 것이 좋습니다.

1

iOS에 파일을 작성하여 & 글을 작성했습니다. 내 .CS 클래스 (ContentPage의 서브 클래스)에서 읽기 위해

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

아이폰 OS에서 보려면 다음을 보면, 아래 LoadAndSave는 아래와 같이 인터페이스는 코드

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

에게 있습니다

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

희망이 도움이됩니다.

+0

입니다. SaveTextAsync()에 텍스트를 저장하고 있습니다. 어떻게 SaveFile()처럼 스트림을 저장할 수 있습니까? – user3034944

+0

또한'tempFileService'와'tempFile.StoredFileName'은 무엇입니까? 나는 Xamarin을 사용하지 않고있다. Form은 그저 XAmarin.iOS – user3034944

+0

나는 당신의 방법을 시도했지만 여전히 나에게 같은 문제를 준다. 내 문제는 파일을 저장하거나 읽는 것이 아닙니다. 파일이 있는지 확인하는 동일한 코드로 처음에는 완벽하게 작동합니다. 파일이 존재하더라도 파일이 존재하는지 여부를 확인할 때 앱에서만 다시 실행됩니다. – user3034944