通过webclient发送byte[]的一部分,在服务器端创建一个大文件



因此,尝试传输一个大字节[],选择将其分成20MB的块,并在收到的第一个块中创建文件并添加该文件,其余的打开现有文件并添加其余的。我遇到的问题是,不是发送第一部分,然后重新连接以接收第二部分,而是同时建立两个连接并发送两个块。第一个完成后,第二个怎么发?

client.OpenWriteAsync(ub.Uri); 
void client_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
{
    if (e.Cancelled)
    {
        MessageBox.Show("Cancelled");
    }
    else if (e.Error != null)
    {
        MessageBox.Show("Deu erro");
    }
    else
    {
        try
        {
            using (Stream output = e.Result)
            {
                int countbytes;
                //for (int i = 0; i < max; i++)
                //{
                if ( (max+1) != maxAux)
                {
                    countbytes = zippedMemoryStream.Read(PartOfDataSet, 0 , 20000000);//maxAux * 20000000
                    output.Write(PartOfDataSet, 0, (int)countbytes);
                    if (max != maxAux)
                    {
                       client.OpenWriteAsync(ub.Uri); 
                    }
                    maxAux++;
                }
                //}
                //numeroimagem++;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            //throw;
        }
    }
}

public void ProcessRequest(HttpContext context)
{
    //context.Response.ContentType = "text/plain";
    //context.Response.Write("Hello World");
    string ImageName = context.Request.QueryString["ImageName"];
    string UploadPath = context.Server.MapPath("~/ServerImages/");
    byte[] bytes = new byte[20000000];
    int bytesToRead = 0;
    if (!File.Exists(UploadPath + ImageName))
    {
        using (FileStream stream = File.Create(UploadPath + ImageName))
        {
            try
            {
                //List<byte> bytes = new List<byte>();

                while ((bytesToRead =
                context.Request.InputStream.Read(bytes, 0, bytes.Length)) != 0)
                //context.Request.InputStream.Read(bytes, 0, 200000)) != 0)
                {
                    stream.Write(bytes, 0, bytesToRead);
                    stream.Close();
                }
                bytes = null;
            }
            catch (Exception ex)
            {
                string error = ex.Message;
                throw;
            }
        }
    }
    else
    {
        using (FileStream stream = File.Open(UploadPath + ImageName,FileMode.Append))
        {
            try
            {
                while ((bytesToRead =
                context.Request.InputStream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    stream.Write(bytes, 0, bytesToRead);
                    stream.Close();
                }
                bytes = null;
            }
            catch (Exception ex)
            {
                string error = ex.Message;
                throw;
            }
        }
    }
}
public bool IsReusable
{
    get
    {
        return false;
    }
}

实际上,再次打开客户端将一无所获。您可以在zippedMemoryStream块中循环并将其写入输出流。然后数据将按顺序到达服务器端。

如果您真的想每次创建一个新连接,请查看UploadData方法

最新更新