1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
private static 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.PostedFile.InputStream.Read(buffer, 0, bufferLength);
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
state.Request.ContentLength = count;
// IMPORTANT: Close the request stream before sending the request.
stream.Close();
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 (WebException e)
{
m_Dlg.ConsoleMessage("Error", string.Format("{0} | Message : {1}", state.FileName, e.Message));
m_Dlg.LogReport.ErrorFTP.Add(string.Format("{0} | Message : {1}", state.FileName, e.Message));
state.OperationException = e;
state.OperationComplete.Set();
return;
}
catch (Exception e)
{
m_Dlg.ConsoleMessage("Error", string.Format("{0} | Message : {1}", state.FileName, e.Message));
state.OperationException = e;
state.OperationComplete.Set();
return;
}
} |
Partager