Example #1
0
    private void EndGetStreamCallback(IAsyncResult ar)
    {
        FtpState state = (FtpState)ar.AsyncState;

        Stream requestStream = null;

        // End the asynchronous call to get the request stream.
        try
        {
            requestStream = state.Request.EndGetRequestStream(ar);
            // Copy the file contents to the request stream.
            const int  bufferLength = 2048;
            byte[]     buffer       = new byte[bufferLength];
            int        count        = 0;
            int        readBytes    = 0;
            FileStream stream       = File.OpenRead(state.FileName);
            do
            {
                readBytes = stream.Read(buffer, 0, bufferLength);
                requestStream.Write(buffer, 0, readBytes);
                count += readBytes;

                if (state.StepProgressDelegate != null)
                {
                    state.StepProgressDelegate(count);
                }
            }while (readBytes != 0);

            Debug.Log("Writing {0} bytes to the stream.", count);

            // IMPORTANT: Close the request stream before sending the request.
            requestStream.Close();

            // Asynchronously get the response to the upload request.
            state.Request.BeginGetResponse(
                new AsyncCallback(EndGetResponseCallback),
                state
                );
        }
        // Return exceptions to the main application thread.
        catch (Exception e)
        {
            Debug.LogError("AsynchronousFtpUpLoader Could not get the request stream. " + e.Message);
            state.OperationException = e;
        }
    }