C# 一旦我们收到分块请求中的分块,是否有一种方法可以处理它们?

C# 一旦我们收到分块请求中的分块,是否有一种方法可以处理它们?,c#,asp.net,chunked,transfer-encoding,C#,Asp.net,Chunked,Transfer Encoding,我希望我能正确地解释我自己。 我有一个带有post方法get#u file()的C#web服务,从客户端,我在服务器中向该方法发出post请求,传输编码分块,并分块发送文件 该方法在请求完成后启动(输入0\r\n\r\n),是否有一种方法可以在将每个块放到服务器后处理它,而不是等到最后 也许发送多个POST请求的替代方案会更好?(但在大约100个请求之后,我得到一个错误) 再说一遍,我希望能解释清楚。 提前感谢据我所知,您希望C#客户端将一个大文件分块上传到托管您的C#服务的服务器上 在本例中,

我希望我能正确地解释我自己。 我有一个带有post方法get#u file()的C#web服务,从客户端,我在服务器中向该方法发出post请求,传输编码分块,并分块发送文件

该方法在请求完成后启动(输入0\r\n\r\n),是否有一种方法可以在将每个块放到服务器后处理它,而不是等到最后

也许发送多个POST请求的替代方案会更好?(但在大约100个请求之后,我得到一个错误)

再说一遍,我希望能解释清楚。
提前感谢

据我所知,您希望C#客户端将一个大文件分块上传到托管您的C#服务的服务器上

在本例中,我已经使用了以下解决方案一段时间,并且它已经证明非常健壮:

服务器端功能:

    [WebMethod]
    public void UploadFile(string FileName, byte[] buffer, long Offset, out bool UploadOK, out string msg)
    {
        Log(string.Format("Upload File {0}. Offset {1}, Bytes {2}...", FileName, Offset, buffer.Length));
        UploadOK = false;
        try
        {
            // setting the file location to be saved in the server. 
            // reading from the web.config file 
            string FilePath = Path.Combine( ConfigurationManager.AppSettings["upload_path"], FileName);

            if (Offset == 0) // new file, create an empty file
                File.Create(FilePath).Close();
            // open a file stream and write the buffer. 
            // Don't open with FileMode.Append because the transfer may wish to 
            // start a different point
            using (FileStream fs = new FileStream(FilePath, FileMode.Open,
                FileAccess.ReadWrite, FileShare.Read))
            {
                fs.Seek(Offset, SeekOrigin.Begin);
                fs.Write(buffer, 0, buffer.Length);
            }
            UploadOK = true;
            msg = "uploaded to " + FilePath;

            Log(string.Format("Sucessfully Uploaded to File {0}: {1}", FileName, msg));
        }
        catch (Exception ex)
        {
            //sending error:
            msg = "failed to upload: " + ex.Message;
            UploadOK = false;
            Log(string.Format("Failed Upload File {0}: {1}", EmlFileName, ex.Message));
        }
    }
static void SendFile(YourWebService webservice, string filename)
    {
        Console.WriteLine("uploading file: " + filename);

        int Offset = 0; // starting offset.

        //define the chunk size
        int ChunkSize = 65536; // 64 * 1024 kb

        //define the buffer array according to the chunksize.
        byte[] Buffer = new byte[ChunkSize];
        //opening the file for read.
        FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
        try
        {
            long FileSize = new FileInfo(filename).Length; // File size of file being uploaded.
            // reading the file.
            fs.Position = Offset;
            int BytesRead = 0;
            string msg = "";
            while (Offset != FileSize) // continue uploading the file chunks until offset = file size.
            {
                BytesRead = fs.Read(Buffer, 0, ChunkSize); // read the next chunk 
                // (if it exists) into the buffer. 
                // the while loop will terminate if there is nothing left to read
                // check if this is the last chunk and resize the buffer as needed 
                // to avoid sending a mostly empty buffer 
                // (could be 10Mb of 000000000000s in a large chunk)
                if (BytesRead != Buffer.Length)
                {
                    ChunkSize = BytesRead;
                    byte[] TrimmedBuffer = new byte[BytesRead];
                    Array.Copy(Buffer, TrimmedBuffer, BytesRead);
                    Buffer = TrimmedBuffer; // the trimmed buffer should become the new 'buffer'
                }
                // send this chunk to the server. it is sent as a byte[] parameter, 
                // but the client and server have been configured to encode byte[] using MTOM. 
                bool ChunkAppened = webservice.UploadFile(Path.GetFileName(filename), Buffer, Offset, out msg);
                if (!ChunkAppened)
                {
                    Console.WriteLine("failed to upload. server return error: " + msg);
                    break;
                }
                // Offset is only updated AFTER a successful send of the bytes. 
                Offset += BytesRead; // save the offset position for resume
            }
            Console.WriteLine("successfully uploaded file: " + filename);
        }
        catch (Exception ex)
        {
            Console.WriteLine("failed to upload file: " + ex.Message);
        }
        finally
        {
            fs.Close();
        }
    }
客户端上传功能:

    [WebMethod]
    public void UploadFile(string FileName, byte[] buffer, long Offset, out bool UploadOK, out string msg)
    {
        Log(string.Format("Upload File {0}. Offset {1}, Bytes {2}...", FileName, Offset, buffer.Length));
        UploadOK = false;
        try
        {
            // setting the file location to be saved in the server. 
            // reading from the web.config file 
            string FilePath = Path.Combine( ConfigurationManager.AppSettings["upload_path"], FileName);

            if (Offset == 0) // new file, create an empty file
                File.Create(FilePath).Close();
            // open a file stream and write the buffer. 
            // Don't open with FileMode.Append because the transfer may wish to 
            // start a different point
            using (FileStream fs = new FileStream(FilePath, FileMode.Open,
                FileAccess.ReadWrite, FileShare.Read))
            {
                fs.Seek(Offset, SeekOrigin.Begin);
                fs.Write(buffer, 0, buffer.Length);
            }
            UploadOK = true;
            msg = "uploaded to " + FilePath;

            Log(string.Format("Sucessfully Uploaded to File {0}: {1}", FileName, msg));
        }
        catch (Exception ex)
        {
            //sending error:
            msg = "failed to upload: " + ex.Message;
            UploadOK = false;
            Log(string.Format("Failed Upload File {0}: {1}", EmlFileName, ex.Message));
        }
    }
static void SendFile(YourWebService webservice, string filename)
    {
        Console.WriteLine("uploading file: " + filename);

        int Offset = 0; // starting offset.

        //define the chunk size
        int ChunkSize = 65536; // 64 * 1024 kb

        //define the buffer array according to the chunksize.
        byte[] Buffer = new byte[ChunkSize];
        //opening the file for read.
        FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
        try
        {
            long FileSize = new FileInfo(filename).Length; // File size of file being uploaded.
            // reading the file.
            fs.Position = Offset;
            int BytesRead = 0;
            string msg = "";
            while (Offset != FileSize) // continue uploading the file chunks until offset = file size.
            {
                BytesRead = fs.Read(Buffer, 0, ChunkSize); // read the next chunk 
                // (if it exists) into the buffer. 
                // the while loop will terminate if there is nothing left to read
                // check if this is the last chunk and resize the buffer as needed 
                // to avoid sending a mostly empty buffer 
                // (could be 10Mb of 000000000000s in a large chunk)
                if (BytesRead != Buffer.Length)
                {
                    ChunkSize = BytesRead;
                    byte[] TrimmedBuffer = new byte[BytesRead];
                    Array.Copy(Buffer, TrimmedBuffer, BytesRead);
                    Buffer = TrimmedBuffer; // the trimmed buffer should become the new 'buffer'
                }
                // send this chunk to the server. it is sent as a byte[] parameter, 
                // but the client and server have been configured to encode byte[] using MTOM. 
                bool ChunkAppened = webservice.UploadFile(Path.GetFileName(filename), Buffer, Offset, out msg);
                if (!ChunkAppened)
                {
                    Console.WriteLine("failed to upload. server return error: " + msg);
                    break;
                }
                // Offset is only updated AFTER a successful send of the bytes. 
                Offset += BytesRead; // save the offset position for resume
            }
            Console.WriteLine("successfully uploaded file: " + filename);
        }
        catch (Exception ex)
        {
            Console.WriteLine("failed to upload file: " + ex.Message);
        }
        finally
        {
            fs.Close();
        }
    }

如果您只想在数据进入时读取数据(但看不到单独的数据块),那么应该会有所帮助。我希望能够立即处理数据块(使用数据块做些事情),这就是我目前拥有的(某种程度上)-向服务器中的某个方法发送POST请求的循环,我代码中的问题是服务器在大约100个请求之后拒绝了我的请求