通过webclient发送一部分字节[]以在服务器端创建一个大文件

时间:2014-06-02 10:27:19

标签: c# silverlight bytearray webclient

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

1 个答案:

答案 0 :(得分:1)

事实上,再次打开客户端你什么也得不到。您可以以块的形式循环zippedMemoryStream并将其写入输出流。然后数据将按顺序到达服务器端。

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