Пример #1
0
        private async Task downloadAndOpen(HsFile item)
        {
            //如果文件存在与本地,则直接打开,否则先下载再打开。

            string[] cachePath = new string[] { "Cache", "Files" };

            string cachefilename = $"{item.FileHash}.{item.FileName}";

            IPlatformExtension pe = DependencyService.Get <IPlatformExtension>(DependencyFetchTarget.GlobalInstance);

            if (!pe.FileExist(cachefilename, cachePath)) //文件不存在需要下载
            {
                int index = 0;

                using (MemoryStream ms = new MemoryStream())
                {
                    while (true)
                    {
                        //下载文件...
                        HsLabelValue result = await GetWSUtil().GetSysFileSeg(GetLoginData().ProgressId, item.FileHash, index);

                        if (result == null) //文件读取完毕
                        {
                            break;
                        }

                        byte[] buffer = HsBase64.FromBase64ToBytes(result.GetValueByLabel("Data"));

                        //数据解压缩
                        buffer = HsZlib.DecompressData(buffer);

                        ms.Write(buffer, 0, buffer.Length);

                        index++;
                    }

                    byte[] fileData = ms.ToArray();

                    Debug.WriteLine(HsMD5.EncryptionMD5(fileData));

                    await pe.WriteFile(cachefilename, fileData, 2, cachePath);
                }

                await downloadAndOpen(item);
            }
            else
            {
                List <string> previewFiletypes = new List <string>()
                {
                    "DOC", "DOCX",
                    "XLS", "XLSX",
                    "PPT", "PPTX",
                    "PDF",
                    "TXT", "TEXT",
                    "JPG", "PNG"
                };

                if (previewFiletypes.Contains(item.FileType.ToUpper()))
                {
                    string url = pe.GetFileURL(cachefilename, cachePath);

                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        Panel_PreviewFile panel = new Panel_PreviewFile();

                        panel.Source = new UrlWebViewSource()
                        {
                            Url = url
                        };

                        await Navigation.PushAsync(panel);
                    }
                }
                else
                {
                    pe.CallFile(cachefilename, cachePath);
                }
            }
        }