诗情寻知己
揽几缕、轻挽起,暮暮朝朝与君语。

主要流程

1. 获取设置的最大传输值

2. 获取文件,转化为文件流

3. 将文件按最大传输值分成byte数组

4. 服务器接收文件流,获取文件续存点

 

用过的场景(单文件上传)

1. WebService上传文件

2. WebApi上传文件

3. 前端上传文件

 

实现代码(C#

1.获取文件GetFileUpload

public void GetFileUpload()

{

       string url = @"地址需要更换";

       //1024字节=1kb,1024kb=1mb,1024mb=1gb

       int def_size = 1024*1024*5;

       byte[] data;

       long current = 0;

       int index = 0;

       bool isEnd = true;

       if (File.Exists(url))

       {

              FileStream fs = File.OpenRead(url);

              Stopwatch sw = new Stopwatch();

              try

              {                                

                     long fileLength = fs.Length;                                                                  

                     sw.Start();

                     while (isEnd)

                     {

                            index++;

                            if (fileLength - current < def_size)

                            {

                                   data = new byte[Convert.ToInt32((fileLength - current))];

                            }

                            else

                            {

                                   data = new byte[def_size];

                            }

                            fs.Read(data, 0, data.Length);

                            current = SaveFile(data, "new.pdf");

                            Console.WriteLine("上传中...");

                            Console.WriteLine("文件上传:" + ((current * 1.0 / fileLength) * 100).ToString("0.00") + "%");

                            if (current >= fileLength)

                            {

                                   isEnd = false;

                                   fs.Close();

                                   sw.Stop();

                                   Console.WriteLine(string.Format("上传完成,文件分为:" + index + "份上传,共用:{0}毫秒", sw.ElapsedMilliseconds));

                            }

                     }

              }

              catch (Exception ex)

              {

                     Console.WriteLine(ex.Message);

              }

              finally

              {

                     fs.Close();

                     sw.Stop();

              }

       }

}

 

2.保存文件SaveFile

public long SaveFile(byte[] content,string fileName)

{

       // 上传的文件续传点。

       long startPoint;

       string path = @"地址需要更换"+ fileName;

       FileStream fs = null;

       try

       {

              if (File.Exists(path))

              {

                     fs = new FileStream(path, FileMode.Append, FileAccess.Write);

                     startPoint = fs.Length;

                     fs.Seek(startPoint, SeekOrigin.Begin);

              }

              else

              {

                     fs = new FileStream(path, FileMode.Create, FileAccess.Write);

                     startPoint = 0;

              }

 

              if (content.Length > 0)

              {

                     byte[] addcontent = content;

                     fs.Write(addcontent, 0, addcontent.Length);

                     startPoint = fs.Length;

              }

       }

       catch (Exception ex)

       {

              throw;

       }

       finally

       {

              fs.Close();

       }

       return startPoint;

}

posted on 2019-08-13 15:48  诗情寻知己  阅读(280)  评论(0)    收藏  举报