1. 程式人生 > >文件分塊傳輸

文件分塊傳輸

exist exists fse 分塊傳輸 index bject align mes readwrite

服務接收端:
public bool AppendChunk(string serverFileName, byte[] buff, long offset, out string errMsg)
{
            errMsg = string.Empty;
            int maxSize =1024 * 1024;//1M
            bool result = false;
            //"D:\MedicalImage\ZipReceived\"
            string ReceivePath = AppSettingHelper.Instance().AppSetting(WcfFolderEnum.RECEIVEFOLDER);
            
string FilePath = Path.Combine(ReceivePath, serverFileName); if (buff != null && buff.Length <= maxSize) { try { if (!Directory.Exists(ReceivePath)) Directory.CreateDirectory(ReceivePath);
if (offset == 0) // 創建新文件 File.Create(FilePath).Close(); // 寫入分塊 using (FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { fs.Seek(offset, SeekOrigin.Begin); fs.Write(buff,
0, buff.Length); } result = true; } catch (Exception ex) { errMsg = string.Format("AppendChunk offset[{0}] error:{1}", offset, ex.Message); } } else { errMsg = "AppendChunk buff error,the count of bytes in buff can not be zero or bigger than " + maxSize; } return result; }


客戶調用端:
OpenFileDialog openFileDialog=new OpenFileDialog();
openFileDialog.InitialDirectory="D:\\";//註意這裏寫路徑時要用c:\\而不是c:\
openFileDialog.Filter="壓縮文件|*.zip;*.rar";
openFileDialog.RestoreDirectory=true;
openFileDialog.FilterIndex=1;
if (openFileDialog.ShowDialog()==DialogResult.OK)
{
    TransitSVC.TransitServiceClient client=new TransitSVC.TransitServiceClient(); 
    string fName=openFileDialog.FileName;
    FileInfo file=new FileInfo(fName);
    FileStream fs = File.Open(fName, FileMode.Open, FileAccess.ReadWrite);
    string msg=String.Empty;
    byte[] bytes = new byte[0];
    BinaryReader r = new BinaryReader(fs);
    int length = (int) fs.Length;
    int size = 10 * 1024;
    if (length > size)//10K一塊
    {
        int chunckCount = length % size == 0 ? length / size : (length / size) + 1;
        for (int i = 0; i < chunckCount; i++)
        {
            try
            {
                r.BaseStream.Seek(i * size, SeekOrigin.Begin);//將文件指針設置到文件開  
                bytes = r.ReadBytes(size);
                if (client.AppendChunk(out msg, file.Name, bytes, i*size))
                {
                    this.showProgress(i+1+"塊傳輸成功");
                }
                else
                {
                    this.showProgress(msg);
                }
            }
            catch (Exception ex)
            {
                this.showProgress(ex.Message);
            }
        }
    }
    else
    {
        try
        {
            r.BaseStream.Seek(0, SeekOrigin.Begin);
            bytes = r.ReadBytes((int)r.BaseStream.Length);
            if (client.AppendChunk(out msg, file.Name, bytes, bytes.Length))
            {
                this.showProgress("傳輸成功");
            }
            else
            {
                this.showProgress(msg);
            }
        }
        catch (Exception ex)
        {
            this.showProgress(ex.Message);
        }
    }
}

protected delegate void ShowProgress(string info);//委托方法記錄信息
protected void showProgress(string text)
{
if (this.InvokeRequired)
{
    ShowProgress h = new ShowProgress(this.showProgress);
    this.BeginInvoke(h, new object[] { text });
    return;
}
this.textBox.Text += text + "\r\n";
this.textBox.SelectionStart = this.textBox3.Text.Length;
this.textBox.SelectionLength = 0;
this.textBox.ScrollToCaret();
}

文件分塊傳輸