Ejemplo n.º 1
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                object[]     obj       = e.Argument as object[];
                TDataInfoDTO dto       = obj[0] as TDataInfoDTO;
                long         totalSize = long.Parse(dto.FileSize);
                if (dto == null)
                {
                    e.Result = null;
                    return;
                }

                long   total   = 0;
                byte[] buffer  = null;
                bool   success = true;
                while (success)
                {
                    using (FileStream stream = new FileStream(obj[1].ToString(), FileMode.Append))
                    {
                        success = _client.TDataInfoDownloadFile(out buffer, total, dto.MetaDataId);
                        if (success == false)
                        {
                            break;
                        }
                        total += buffer.Length;
                        backgroundWorker1.ReportProgress((int)(total * 100 / totalSize));
                        stream.Write(buffer, 0, buffer.Length);
                    }
                }
                e.Result = obj[1].ToString();
            }
            catch (Exception)
            {
                e.Result = null;
            }
        }
Ejemplo n.º 2
0
        public static int GetImageIndex(TDataInfoDTO dto, ImageList lstImg)
        {
            string fileExtention = Path.GetExtension(dto.DataName);
            string keyword       = fileExtention;

            try
            {
                if (!lstImg.Images.Keys.Contains(fileExtention))
                {
                    //图片文件,寻找图片缩略图
                    if (fileExtention.ToLower().EndsWith(".png") || fileExtention.ToLower().EndsWith(".bmp") || fileExtention.ToLower().EndsWith(".jpeg") || fileExtention.ToLower().EndsWith(".jpg"))
                    {
                        keyword = dto.MetaDataId;
                        Image  targetImg = null;
                        string tempDir   = Path.Combine(System.Environment.GetEnvironmentVariable("TEMP"), "64");
                        if (!Directory.Exists(tempDir))
                        {
                            Directory.CreateDirectory(tempDir);
                        }
                        string tempPath = Path.Combine(tempDir, dto.MetaDataId + ".jpeg");
                        if (!File.Exists(tempPath))
                        {
                            //下载缩略图
                            string temp    = Path.Combine(System.Environment.GetEnvironmentVariable("TEMP"), dto.MetaDataId + fileExtention);
                            long   total   = 0;
                            byte[] buffer  = null;
                            bool   success = true;
                            while (success)
                            {
                                using (FileStream stream = new FileStream(temp, FileMode.Append))
                                {
                                    success = _client.TDataInfoDownloadFile(out buffer, total, dto.MetaDataId);
                                    if (success == false)
                                    {
                                        break;
                                    }
                                    total += buffer.Length;
                                    stream.Write(buffer, 0, buffer.Length);
                                }
                            }
                            Image sourceImg = Image.FromFile(temp);
                            int   width     = sourceImg.Width * 64 / sourceImg.Height;
                            targetImg = GetReducedImage(sourceImg, width, 64);
                            targetImg.Save(tempPath, ImageFormat.Jpeg);
                            // File.Delete(temp);
                        }
                        else
                        {
                            targetImg = Image.FromFile(tempPath);
                        }
                        lstImg.Images.Add(keyword, targetImg);
                    }
                    else
                    {
                        keyword = fileExtention;
                        lstImg.Images.Add(keyword, IconsExtention.IconFromExtension(fileExtention, IconsExtention.SystemIconSize.Large));
                    }
                }
            }
            catch (Exception)
            {
                if (fileExtention.EndsWith(".exe"))
                {
                    keyword = fileExtention;
                    lstImg.Images.Add(keyword, Resources.exe);
                }
            }
            return(lstImg.Images.IndexOfKey(keyword));
        }
Ejemplo n.º 3
0
        private void _bgwork_DoWork(object sender, DoWorkEventArgs e)
        {
            if (OpareteType == OpareteTypeEnum.Upload)
            {
                try
                {
                    object[] arg     = e.Argument as object[];
                    byte[]   total   = arg[0] as byte[];
                    string   id      = arg[1].ToString();
                    byte[]   buffer  = new byte[409600]; //400kb
                    int      percent = total.Length / buffer.Length;
                    for (long index = 0; index < percent; index++)
                    {
                        buffer = total.Skip((int)index * buffer.Length).Take(buffer.Length).ToArray();
                        bool result = _client.TDataInfoUploadFile(buffer, id, (int)(index * buffer.Length / 40960000));
                        if (result == false)
                        {
                            e.Result = false;
                            return;
                        }
                        _bgwork.ReportProgress((int)((index + 1) * buffer.Length * 100 / total.Length));
                    }
                    int left = total.Length % buffer.Length;
                    if (left > 0)
                    {
                        byte[] leftBuffer = total.Skip(percent * buffer.Length).Take(left).ToArray();
                        bool   result     = _client.TDataInfoUploadFile(leftBuffer, id, total.Length / 40960000);
                        if (result == false)
                        {
                            e.Result = false;
                            return;
                        }
                        _bgwork.ReportProgress(100);
                    }
                    if (_saveHistory.ToLower().EndsWith(".doc") || _saveHistory.ToLower().EndsWith(".docx") || _saveHistory.ToLower().EndsWith(".ppt") || _saveHistory.ToLower().EndsWith(".pptx") || _saveHistory.ToLower().EndsWith(".xls"))
                    {
                        _client.TDataInDoConvert(id);
                    }

                    e.Result = true;
                }
                catch (Exception)
                {
                    e.Result = false;
                }
            }
            else
            {
                try
                {
                    object[] obj = e.Argument as object[];
                    if (ItemData == null)
                    {
                        e.Result = null;
                        return;
                    }
                    long   totalSize = long.Parse(ItemData.FileSize);
                    long   total     = 0;
                    byte[] buffer    = null;
                    if (File.Exists(obj[0].ToString()))
                    {
                        File.Delete(obj[0].ToString());
                    }
                    bool success = true;
                    while (success)
                    {
                        using (FileStream stream = new FileStream(obj[0].ToString(), FileMode.Append))
                        {
                            success = _client.TDataInfoDownloadFile(out buffer, total, ItemData.MetaDataId);
                            if (success == false)
                            {
                                break;
                            }
                            total += buffer.Length;
                            _bgwork.ReportProgress((int)(total * 100 / totalSize));
                            stream.Write(buffer, 0, buffer.Length);
                        }
                    }

                    e.Result = (total == totalSize);
                }
                catch (Exception)
                {
                    e.Result = false;
                }
            }
        }