É
éric
Hi,
below is the code I sort of adapted from an async http download example I
found so that I could upload files to the server.
It seems to be doing it asynchronously and the file makes it to the server
but it is going all in one shot.
How can trap it at every 1024 bytes to trigger an event like progress bar
advance as it does in the download version of this code?
When the code calls back uReadCallBack for the first time the entire file is
already on the server?
Any ideas?
-----------Code Start-------------
internal class UploadInfo
{
const int BufferSize = 1024;
internal byte[] BufferWrite;
internal bool useFastBuffers;
internal byte[] dataBufferFast;
internal System.Collections.ArrayList dataBufferSlow;
internal int dataLength;
internal int bytesProcessed;
internal HttpWebRequest Request;
internal Stream ResponseStream;
internal DownloadProgressHandler ProgressCallback;
internal UploadInfo()
{
BufferWrite = new byte[BufferSize];
Request = null;
bytesProcessed = 0;
useFastBuffers = true;
}
}
internal class WebDownload
{
private event System.EventHandler eventStop;
private event ECASOFT.Controls.DownloadError eventError;
internal ManualResetEvent allDone = new ManualResetEvent(false);
internal bool Abort;
private WebRequest req;
const int BUFFER_SIZE = 1024;
internal event System.EventHandler Aborted
{
add {eventStop += new System.EventHandler (value);}
remove {eventStop -= new System.EventHandler (value);}
}
internal event ECASOFT.Controls.DownloadError DownloadError
{
add {eventError += new ECASOFT.Controls.DownloadError (value);}
remove {eventError -= new ECASOFT.Controls.DownloadError (value);}
}
// Start the upload
internal byte[] Upload(
string url, string username, string password, string domain,
DownloadProgressHandler progressCB)
{
try
{
Abort = false;
allDone.Reset();
Uri httpSite = new Uri(url);
FileStream rdr = new FileStream(\\My File.txt, FileMode.Open);
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
req.Method = "PUT";
req.PreAuthenticate = true;
req.Credentials = new NetworkCredential(username, password,
domain);
req.AllowWriteStreamBuffering = true;
req.ContentLength = rdr.Length;
Stream reqStream = req.GetRequestStream();
byte[] inData = new byte[4096];
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
reqStream.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
}
UploadInfo info = new UploadInfo();
info.Request = (HttpWebRequest) req;
info.dataLength = 1024;
rdr.Close();
reqStream.Close();
info.ProgressCallback += progressCB;
IAsyncResult r = (IAsyncResult) req.BeginGetResponse(new
AsyncCallback(uResponseCallback), info);
allDone.WaitOne();
byte[] data;
if ( info.useFastBuffers )
{
data = info.dataBufferFast;
info.dataBufferFast = null;
}
else
{
data = new byte[ info.dataBufferSlow.Count ];
for ( int b=0; b<info.dataBufferSlow.Count; b++ )
data = (byte) info.dataBufferSlow;
info.dataBufferSlow = null;
}
if (this.Abort == false)
{
return data;
}
else
{
return new byte[0];
}
}
catch(System.Exception err)
{
return null;
}
}
private void uResponseCallback(IAsyncResult ar)
{
try
{
UploadInfo info = (UploadInfo) ar.AsyncState;
req = info.Request;
WebResponse resp = req.EndGetResponse(ar);
string strContentLength = resp.Headers["Content-Length"];
if ( strContentLength != null )
{
info.dataLength = Convert.ToInt32( strContentLength );
info.dataBufferFast = new byte[ info.dataLength ];
}
else
{
info.useFastBuffers = false;
info.dataBufferSlow = new System.Collections.ArrayList(
BUFFER_SIZE );
}
Stream ResponseStream = resp.GetResponseStream();
info.ResponseStream = ResponseStream;
IAsyncResult iarWrite =
ResponseStream.BeginRead(info.BufferWrite,
0,
BUFFER_SIZE,
new AsyncCallback(uReadCallBack),
info);
}
catch(System.Exception err)
{
}
}
private void uReadCallBack(IAsyncResult asyncResult)
{
try
{
UploadInfo info = (UploadInfo) asyncResult.AsyncState;
Stream responseStream = info.ResponseStream;
if (Abort == true)
{
req.Abort();
req = null;
System.Object o = new object();
eventStop(o, System.EventArgs.Empty);
allDone.Set();
}
else
{
int bytesWriten = responseStream.EndRead(asyncResult );
if (bytesWriten > 0)
{
if ( info.useFastBuffers )
{
System.Array.Copy(info.BufferWrite, 0,
info.dataBufferFast, info.bytesProcessed,
bytesWriten );
}
else
{
for ( int b=0; b<bytesWriten; b++ )
info.dataBufferSlow.Add( info.BufferWrite );
}
info.bytesProcessed += bytesWriten;
if ( info.ProgressCallback != null )
info.ProgressCallback( info.bytesProcessed,
info.dataLength);
IAsyncResult ar = responseStream.BeginRead(
info.BufferWrite, 0, BUFFER_SIZE,
new AsyncCallback(uReadCallBack), info);
}
else
{
responseStream.Close();
allDone.Set();
}
}
}
catch(System.Exception err)
{
}
}
#endregion
below is the code I sort of adapted from an async http download example I
found so that I could upload files to the server.
It seems to be doing it asynchronously and the file makes it to the server
but it is going all in one shot.
How can trap it at every 1024 bytes to trigger an event like progress bar
advance as it does in the download version of this code?
When the code calls back uReadCallBack for the first time the entire file is
already on the server?
Any ideas?
-----------Code Start-------------
internal class UploadInfo
{
const int BufferSize = 1024;
internal byte[] BufferWrite;
internal bool useFastBuffers;
internal byte[] dataBufferFast;
internal System.Collections.ArrayList dataBufferSlow;
internal int dataLength;
internal int bytesProcessed;
internal HttpWebRequest Request;
internal Stream ResponseStream;
internal DownloadProgressHandler ProgressCallback;
internal UploadInfo()
{
BufferWrite = new byte[BufferSize];
Request = null;
bytesProcessed = 0;
useFastBuffers = true;
}
}
internal class WebDownload
{
private event System.EventHandler eventStop;
private event ECASOFT.Controls.DownloadError eventError;
internal ManualResetEvent allDone = new ManualResetEvent(false);
internal bool Abort;
private WebRequest req;
const int BUFFER_SIZE = 1024;
internal event System.EventHandler Aborted
{
add {eventStop += new System.EventHandler (value);}
remove {eventStop -= new System.EventHandler (value);}
}
internal event ECASOFT.Controls.DownloadError DownloadError
{
add {eventError += new ECASOFT.Controls.DownloadError (value);}
remove {eventError -= new ECASOFT.Controls.DownloadError (value);}
}
// Start the upload
internal byte[] Upload(
string url, string username, string password, string domain,
DownloadProgressHandler progressCB)
{
try
{
Abort = false;
allDone.Reset();
Uri httpSite = new Uri(url);
FileStream rdr = new FileStream(\\My File.txt, FileMode.Open);
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
req.Method = "PUT";
req.PreAuthenticate = true;
req.Credentials = new NetworkCredential(username, password,
domain);
req.AllowWriteStreamBuffering = true;
req.ContentLength = rdr.Length;
Stream reqStream = req.GetRequestStream();
byte[] inData = new byte[4096];
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
reqStream.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
}
UploadInfo info = new UploadInfo();
info.Request = (HttpWebRequest) req;
info.dataLength = 1024;
rdr.Close();
reqStream.Close();
info.ProgressCallback += progressCB;
IAsyncResult r = (IAsyncResult) req.BeginGetResponse(new
AsyncCallback(uResponseCallback), info);
allDone.WaitOne();
byte[] data;
if ( info.useFastBuffers )
{
data = info.dataBufferFast;
info.dataBufferFast = null;
}
else
{
data = new byte[ info.dataBufferSlow.Count ];
for ( int b=0; b<info.dataBufferSlow.Count; b++ )
data = (byte) info.dataBufferSlow;
info.dataBufferSlow = null;
}
if (this.Abort == false)
{
return data;
}
else
{
return new byte[0];
}
}
catch(System.Exception err)
{
return null;
}
}
private void uResponseCallback(IAsyncResult ar)
{
try
{
UploadInfo info = (UploadInfo) ar.AsyncState;
req = info.Request;
WebResponse resp = req.EndGetResponse(ar);
string strContentLength = resp.Headers["Content-Length"];
if ( strContentLength != null )
{
info.dataLength = Convert.ToInt32( strContentLength );
info.dataBufferFast = new byte[ info.dataLength ];
}
else
{
info.useFastBuffers = false;
info.dataBufferSlow = new System.Collections.ArrayList(
BUFFER_SIZE );
}
Stream ResponseStream = resp.GetResponseStream();
info.ResponseStream = ResponseStream;
IAsyncResult iarWrite =
ResponseStream.BeginRead(info.BufferWrite,
0,
BUFFER_SIZE,
new AsyncCallback(uReadCallBack),
info);
}
catch(System.Exception err)
{
}
}
private void uReadCallBack(IAsyncResult asyncResult)
{
try
{
UploadInfo info = (UploadInfo) asyncResult.AsyncState;
Stream responseStream = info.ResponseStream;
if (Abort == true)
{
req.Abort();
req = null;
System.Object o = new object();
eventStop(o, System.EventArgs.Empty);
allDone.Set();
}
else
{
int bytesWriten = responseStream.EndRead(asyncResult );
if (bytesWriten > 0)
{
if ( info.useFastBuffers )
{
System.Array.Copy(info.BufferWrite, 0,
info.dataBufferFast, info.bytesProcessed,
bytesWriten );
}
else
{
for ( int b=0; b<bytesWriten; b++ )
info.dataBufferSlow.Add( info.BufferWrite );
}
info.bytesProcessed += bytesWriten;
if ( info.ProgressCallback != null )
info.ProgressCallback( info.bytesProcessed,
info.dataLength);
IAsyncResult ar = responseStream.BeginRead(
info.BufferWrite, 0, BUFFER_SIZE,
new AsyncCallback(uReadCallBack), info);
}
else
{
responseStream.Close();
allDone.Set();
}
}
}
catch(System.Exception err)
{
}
}
#endregion