예제 #1
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;
                }
            }
        }