Forum Xamarin.iOS

how to download a PPt file on xamarin ios?

ragibNoorragibNoor Member ✭✭
edited August 2020 in Xamarin.iOS

I am working on an xamarin application where I have to download a PPT file and opened the downloded ppt file from PowerPoint app (if available on a device)

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    Try to use HttpClient to get the bytes from the url.

    async Task<byte[]> DownloadImageAsync(string imageUrl)
    {
        var _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(15) };
    
        try
        {
            using (var httpResponse = await _httpClient.GetAsync(imageUrl))
            {
                if (httpResponse.StatusCode == HttpStatusCode.OK)
                {
                    return await httpResponse.Content.ReadAsByteArrayAsync();
                }
                else
                {
                    //Url is Invalid
                    return null;
                }
            }
        }
        catch (Exception)
        {
            //Handle Exception
            return null;
        }
    }
    

    Then get the byte arry from the method and write to the file with the File.WriteAllBytes command.

    Similar issue:
    https://forums.xamarin.com/discussion/138266/how-to-hit-an-url-to-download-a-file

  • ragibNoorragibNoor Member ✭✭

    hi @YelinZh
    how to store files in the public folder (like android download folder) for ios app what will be the file path for the following method "File.WriteAllBytes(YourFilePath, ReturnedBytes)"?

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    how to store files in the public folder (like android download folder) for ios app what will be the file path ...

    To save the file to the public folder, try to achieve the function on each platform. Then use DependencyService to call the method in the shared project.

    Check the code:

    [assembly: Dependency(typeof(SaveFileClass))]
    namespace App19F_9.Droid
    {
        public class SaveFileClass : ISaveFile
        {
            public void saveFile(byte[] bytes)
            {
                var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
                string file_name = System.IO.Path.Combine(path.ToString(),"test.txt");
    
                File.WriteAllBytes(file_name,bytes);
            }
        }
    }
    

    Invoke the function code

    DependencyService.Get<ISaveFile>().saveFile(bytes);
    
  • ragibNoorragibNoor Member ✭✭

    Thanks @YelinZh
    could you please share sample code for ios I am looking for ios solution

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    iOS imposes some restrictions on what an application can do with the file system to preserve the security of an application’s data, and to protect users from malignant apps. It doesn't support the public folder in ios, the file can only be save in the application.

    var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
    var library = Path.Combine (documents, "..", "Library");
    var filename = Path.Combine (library, "WriteToLibrary.txt");
    File.WriteAllBytes(filename, bytes);
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system

  • ragibNoorragibNoor Member ✭✭

    @YelinZh said:
    iOS imposes some restrictions on what an application can do with the file system to preserve the security of an application’s data, and to protect users from malignant apps. It doesn't support the public folder in ios, the file can only be save in the application.

    var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
    var library = Path.Combine (documents, "..", "Library");
    var filename = Path.Combine (library, "WriteToLibrary.txt");
    File.WriteAllBytes(filename, bytes);
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system

    SO in IOS it is not possible to store file in public folder so that user can browse the file on phone ?

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited August 2020

    SO in IOS it is not possible to store file in public folder

    Yes, it's not supported in ios. We can only get the files in the corresponding application. To open the pdf with 'PowerPoint', you need to get the file in the app and then select to open it with another application.

Sign In or Register to comment.