public HsImage(string imageDataString) { if (imageDataString.Split(',').Length > 1) { throw new HsException("无法识别的旧的图像格式"); } byte[] buffers = HsBase64.FromBase64ToBytes(imageDataString); byte[] datas = new byte[buffers.Length - METALEN]; using (MemoryStream bufferStream = new MemoryStream(buffers)) { int endian = buffers[0]; using (BinaryReader br = new BinaryReader(bufferStream, endian == ENDIAN_BIG ? Encoding.BigEndianUnicode : Encoding.Unicode)) { br.ReadByte(); //越过第一位 int format = br.ReadInt32(); //读取2-5位 if (format == PIXEL) { throw new HsException("不支持PIXEL图像格式"); } bufferStream.Seek(METALEN, SeekOrigin.Begin); bufferStream.Read(datas, 0, datas.Length); } } this._imageDataString = imageDataString; this._imageData = ImageSource.FromStream(() => { return(new MemoryStream(datas)); }); }
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); } } }