Question

I'm just new in this PCL libraries, I'm developing an iPhone app with Xamarin and I can't find the way to save it on the phone. The closest I get is with PCLStorage but he only saves text. There is another way that I can save binary files with other procedure. Thank you.

foreach (images element in json_object)
{
            //var nameFile = Path.Combine (directoryname, element.name);
            try{
                IFile file = await folder_new.GetFileAsync(element.name);
            }catch(FileNotFoundException ex ){
                RestClient _Client = new RestClient(element.root);
                RestRequest request_file = new RestRequest("/images/{FileName}");
                request_file.AddParameter("FileName", element.name, ParameterType.UrlSegment);
                _Client.ExecuteAsync<MemoryStream>(
                        request_file,
                        async Response =>
                        {
                                if (Response != null)
                                {
                                    IFolder rootFolder_new = FileSystem.Current.LocalStorage;
                                    IFile file_new = await rootFolder_new.CreateFileAsync(element.name,CreationCollisionOption.ReplaceExisting);
                                    await file_new.WriteAllTextAsync(Response.Data);
                                }
                        });
            }
        }
Was it helpful?

Solution

Use the IFile.OpenAsync method to get a stream which you can use to read/write binary data. Here's how you would read a file:

IFile file = await folder_new.GetFileAsync(element.name);
using (Stream stream = await file.OpenAsync(FileAccess.Read))
{
    //  Read stream and process binary data from it...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top