Thanks for your reply John,
As for how to programmaticall y interact with the upload page in client .net
application, have you had a chance to see the code in the article I
provided or my attached demo? I'm sorry for my carelessness since I
assumed that you'll have a look at those article or my demo. Anyway, here
are some code snippet on using webclient or httpwebrequest to post file
stream:
#using WebClient
string url = "http://myserver/myapp/upload.aspx";
string file = "c:\\files\\tes t.jpg";
WebClient wc = new WebClient();
wc.UploadFile(u rl,"post",file) ;
#using httpWebrequest:
private void UploadFilesToRe moteUrl(string url, string[] files, string
logpath)
{
long length = 0;
string boundary = "----------------------------" +
DateTime.Now.Ti cks.ToString("x ");
HttpWebRequest httpWebRequest2 = (HttpWebRequest )WebRequest.Cre ate(url);
httpWebRequest2 .ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2 .Method = "POST";
httpWebRequest2 .KeepAlive = true;
httpWebRequest2 .Credentials =
System.Net.Cred entialCache.Def aultCredentials ;
Stream memStream = new System.IO.Memor yStream();
byte[] boundarybytes = System.Text.Enc oding.ASCII.Get Bytes("\r\n--" +
boundary + "\r\n");
memStream.Write (boundarybytes, 0,boundarybytes .Length);
length += boundarybytes.L ength;
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\";
filename=\"{1}\ "\r\n Content-Type: application/octet-stream\r\n\r\n" ;
for(int i=0;i<files.Len gth;i++)
{
string header = string.Format(h eaderTemplate," file"+i,files[i]);
byte[] headerbytes = System.Text.Enc oding.UTF8.GetB ytes(header);
memStream.Write (headerbytes,0, headerbytes.Len gth);
length += headerbytes.Len gth;
FileStream fileStream = new FileStream(file s[i], FileMode.Open,
FileAccess.Read );
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ( (bytesRead = fileStream.Read (buffer, 0, buffer.Length)) != 0 )
{
memStream.Write (buffer, 0, bytesRead);
length += bytesRead;
}
memStream.Write (boundarybytes, 0,boundarybytes .Length);
length += boundarybytes.L ength;
fileStream.Clos e();
}
httpWebRequest2 .ContentLength = memStream.Lengt h;
Stream requestStream = httpWebRequest2 .GetRequestStre am();
memStream.Posit ion = 0;
byte[] tempBuffer = new byte[memStream.Lengt h];
memStream.Read( tempBuffer,0,te mpBuffer.Length );
memStream.Close ();
requestStream.W rite(tempBuffer ,0,tempBuffer.L ength);
requestStream.C lose();
WebResponse webResponse2 = httpWebRequest2 .GetResponse();
Stream stream2 = webResponse2.Ge tResponseStream ();
StreamReader reader2 = new StreamReader(st ream2);
MessageBox.Show (reader2.ReadTo End());
webResponse2.Cl ose();
httpWebRequest2 = null;
webResponse2 = null;
}
As we can see, weclient has encapsulated the underlying details of using
httpwebrequest to post data and the code will be very simple. However,
using httpwebrequest directly will give us more control over the posted
data stream as my above function inject multi file stream into the single
HttP request message.
Hope helps.
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)