Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. File upload using http

File upload using http

Scheduled Pinned Locked Moved C#
sysadminhelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sebastian T Xavier
    wrote on last edited by
    #1

    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

    P D 2 Replies Last reply
    0
    • S Sebastian T Xavier

      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

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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 to http://localhost:3040/, you would need to supply http://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

      S 1 Reply Last reply
      0
      • S Sebastian T Xavier

        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

        D Offline
        D Offline
        DonDiegoDeLaVega
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • D DonDiegoDeLaVega

          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

          S Offline
          S Offline
          Sebastian T Xavier
          wrote on last edited by
          #4

          Yeah, It should serve the purpose .... Let me try to implement it Best regards Sebastian

          1 Reply Last reply
          0
          • P Pete OHanlon

            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 to http://localhost:3040/, you would need to supply http://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

            S Offline
            S Offline
            Sebastian T Xavier
            wrote on last edited by
            #5

            Thanks a lot.... Its a great help Let me try to implement it Best regards Sebastian

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups