【问题标题】:Send parts of a byte[] by webclient to create a large file in server side通过 webclient 发送部分 byte[] 以在服务器端创建一个大文件
【发布时间】:2014-06-02 10:27:18
【问题描述】:

因此尝试传输一个大字节 [] 选择将其分隔为 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;
    }
}

【问题讨论】:

    标签: c# silverlight bytearray webclient


    【解决方案1】:

    事实上,再次打开客户端并没有任何好处。您可以在块中循环您的 zippedMemoryStream 并将其写入输出流。然后数据会按顺序到达服务器端。

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

    【讨论】:

    • 但是当我第二次调用client.OpenWriteAsync(ub.Uri); 时,那是client_OpenWriteCompleted 内部的那个,假设在第二次调用连接之前先发送了output.Write(PartOfDataSet, 0, (int)countbytes);
    • 事实上,再次打开客户端并没有任何收获。您可以在块中循环您的 zippedMemoryStream 并将其写入输出流。然后数据会按顺序到达服务器端。
    • 如果您真的想每次都创建一个新连接,请查看 UploadData 方法。
    • 是的,您的方向可能是正确的,谢谢,将尝试这样做,仅在一个连接中发送。
    猜你喜欢
    • 2015-06-25
    • 1970-01-01
    • 2011-05-29
    • 2017-08-04
    • 2013-08-25
    • 1970-01-01
    • 2019-01-25
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多