File upload using http
-
Hello All, I need to upload files to a server using http in a windows application. Is that possible? I have got some examples using HttpPostedFile , but that is in web application. The code is given below...
if( filMyFile.PostedFile != null )
{
HttpPostedFile myFile = filMyFile.PostedFile;
int nFileLen = myFile.ContentLength;
if( nFileLen > 0 )
{
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
string strFilename =
Path.GetFileName(myFile.FileName);
FileStream newFile = new FileStream(strPath,
FileMode.Create);
newFile.Write(Buffer, 0, Buffer.Length);
newFile.Close();
}
}I have placed an openfiledialog in a windows form and used the following code..
FileInfo fileInf = new FileInfo(openFileDialog1.FileName);
But how I can assign this FileInfo to HttpPostedFile. Any help would be appreciated. Thanks in advance... Sebastian
-
Hello All, I need to upload files to a server using http in a windows application. Is that possible? I have got some examples using HttpPostedFile , but that is in web application. The code is given below...
if( filMyFile.PostedFile != null )
{
HttpPostedFile myFile = filMyFile.PostedFile;
int nFileLen = myFile.ContentLength;
if( nFileLen > 0 )
{
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
string strFilename =
Path.GetFileName(myFile.FileName);
FileStream newFile = new FileStream(strPath,
FileMode.Create);
newFile.Write(Buffer, 0, Buffer.Length);
newFile.Close();
}
}I have placed an openfiledialog in a windows form and used the following code..
FileInfo fileInf = new FileInfo(openFileDialog1.FileName);
But how I can assign this FileInfo to HttpPostedFile. Any help would be appreciated. Thanks in advance... Sebastian
Possibly the simplest way for you to do this would be to use the
WebClient
class. Here's a quick sample:private void SendFile(Uri address, string fileName)
{
using (WebClient client = new WebClient())
{
client.UploadFile(address, fileName);
}
}If you supply an HTTP address as the first parameter, POST is used. The beauty of this method is that it's clever enough to use
STOR
if the address is an FTP address, so you get the best of both worlds here. The address has to be the fully qualified URI of the resource to receive the file; this means you have to give it the full address - so if you were writing a file called myfile.csv tohttp://localhost:3040/
, you would need to supplyhttp://localhost:3040/myfile.csv
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hello All, I need to upload files to a server using http in a windows application. Is that possible? I have got some examples using HttpPostedFile , but that is in web application. The code is given below...
if( filMyFile.PostedFile != null )
{
HttpPostedFile myFile = filMyFile.PostedFile;
int nFileLen = myFile.ContentLength;
if( nFileLen > 0 )
{
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
string strFilename =
Path.GetFileName(myFile.FileName);
FileStream newFile = new FileStream(strPath,
FileMode.Create);
newFile.Write(Buffer, 0, Buffer.Length);
newFile.Close();
}
}I have placed an openfiledialog in a windows form and used the following code..
FileInfo fileInf = new FileInfo(openFileDialog1.FileName);
But how I can assign this FileInfo to HttpPostedFile. Any help would be appreciated. Thanks in advance... Sebastian
Hi, 'HttpPostedFile' is not the thing you need , it stands for the file once it has been posted. I guess http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx is what you are looking for
-
Hi, 'HttpPostedFile' is not the thing you need , it stands for the file once it has been posted. I guess http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx is what you are looking for
Yeah, It should serve the purpose .... Let me try to implement it Best regards Sebastian
-
Possibly the simplest way for you to do this would be to use the
WebClient
class. Here's a quick sample:private void SendFile(Uri address, string fileName)
{
using (WebClient client = new WebClient())
{
client.UploadFile(address, fileName);
}
}If you supply an HTTP address as the first parameter, POST is used. The beauty of this method is that it's clever enough to use
STOR
if the address is an FTP address, so you get the best of both worlds here. The address has to be the fully qualified URI of the resource to receive the file; this means you have to give it the full address - so if you were writing a file called myfile.csv tohttp://localhost:3040/
, you would need to supplyhttp://localhost:3040/myfile.csv
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Thanks a lot.... Its a great help Let me try to implement it Best regards Sebastian