Save file to device with memorystream

蹲街弑〆低调 提交于 2019-12-11 15:47:13

问题


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:


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



来源:https://stackoverflow.com/questions/47410562/save-file-to-device-with-memorystream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!