Webclient behaviour using httpwebrequest

G

Guest

I need to upload a file to a server from both pda and desktop machines and through a web interface
The web interface was easy enough in asp.net, and after looking at far too many how-tos and newsgroups I finally managed to get WebClient to post a file to the same form using uploadfile

I'd like to be able to use the same page/server code to deal with all clients, but the compact framework doesn't have the webclient class.
Is there some way i can use httpwebrequest (or something else) to perform a file upload in the same manner as using webclient
 
É

éric

this does it...

internal static bool UploadBinary(string localFile)

{

try

{

string url = URL;



char[] chr = @"/\".ToCharArray();

int strL = localFile.LastIndexOfAny(chr) + 1;

url += localFile.Substring(strL, localFile.Length - strL);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);


req.Timeout = 60000;

req.Method = "PUT";

req.Credentials = new NetworkCredential(

ECASOFT.Controls.Static.ApplicationSettings.RepLogin,

ECASOFT.Controls.Static.ApplicationSettings.RepPassword);

req.AllowWriteStreamBuffering = true;

// Retrieve request stream

Stream reqStream = req.GetRequestStream();

// Open the local file

FileStream rdr = new FileStream(localFile, FileMode.Open);

// Allocate byte buffer to hold file contents

byte[] inData = new byte[4096];

// loop through the local file reading each data block

// and writing to the request stream buffer

int bytesRead = rdr.Read(inData, 0, inData.Length);

while (bytesRead > 0)

{

reqStream.Write(inData, 0, bytesRead);

bytesRead = rdr.Read(inData, 0, inData.Length);

}

rdr.Close();

reqStream.Close();

req.GetResponse();

return true;

}

catch

{

return false;

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top