C# 客服端上传文件与服务器器端接收 (简单代码)

时间:2022-05-18 05:03:59

 简单代码:

    /*服务器端接收写入 可以实现断点续传*/
    public  string  ConnectUpload(string newfilename,string filepath,byte[] fileByte)  
    {
    try{
          string path = adpath + newfilename;                     
          FileStream fs = new FileStream(path, FileMode.Append);
          BinaryWriter w = new BinaryWriter(fs);                     
          fs.Position = fs.Length;                     
          fs.Write(fileByte, 0, fileByte.Length);                     
          w.Close();
          fs.Close();
          return "1";
       }
      catch{
        return "-1";   
       }
    }

    /*客户端循环添加*/
    int count = 0;
    int bufferSize = 256;        
    byte[] buffer = new byte[bufferSize];
    while (count < FileUpload1.FileContent.Length)        
    {            
        int bytes = FileUpload1.PostedFile.InputStream.Read(buffer, 0, bufferSize);            
        string result = ConnectUpload(newfilename, filepath, buffer);            
        if (result != "1") //报错            
        {        
             break;
        }    
   }