文件分块传输

Posted 卓扬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件分块传输相关的知识,希望对你有一定的参考价值。

服务接收端: 
 
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();
}

 

以上是关于文件分块传输的主要内容,如果未能解决你的问题,请参考以下文章

分块上传和断点续传

分块上传和断点续传

2-WAF主要过滤方式及绕过(HPP污染&分块传输&垃圾数据)

使用分块传输通过 HTTP POST 流式传输麦克风输出

HTTP 断点续传(分块传输)

分块编码和内容长度标头