Save file to device with memorystream

2019-08-16 18:08发布

I was wondering if it is possible with xamarin.forms to download any type of file to the device.. de files are stored on Azure, i get a Memorystream of the file, its very important for my app. my question excists of 2 parts actually, how to download de file to the device of the user?, and how to show the file of Any type in a default application of the type ( like pdf reader)

this is what i tried

MemoryStream memoryStream = AzureDownloader.DownloadFromAzureBlobStorage(null, doc.azure_container, doc.file_path, ref filen, true);

  string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  string localFilename = doc.filename;
  string localPath = Path.Combine(documentsPath, localFilename);
  File.WriteAllBytes(localPath, memoryStream.ToArray()); // this is a try to save to local storage

any help appreciated

1条回答
对你真心纯属浪费
2楼-- · 2019-08-16 18:31

how to download the file to the device of the user?

Use PCLStorage as its cross-platform and would work for iOS and Android:

public async Task CreateRealFileAsync()
{
    // get hold of the file system
    IFolder rootFolder = FileSystem.Current.LocalStorage;

    // create a folder, if one does not exist already
    IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder", CreationCollisionOption.OpenIfExists);

    // create a file, overwriting any existing file
    IFile file = await folder.CreateFileAsync("MyFile.txt", CreationCollisionOption.ReplaceExisting);

    // populate the file with some text
    await file.WriteAllTextAsync("Sample Text...");
}

how to show the file of Any type in a default application of the type ( like pdf reader)

this is too broad and could have several solutions depending on what exactly you want to achieve.

Hope this helps

查看更多
登录 后发表回答