4 Replies - 6775 Views - Last Post: 13 September 2011 - 08:39 PM Rate Topic: -----

#1 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

Upload file to server using Http web request

Posted 29 August 2011 - 04:32 AM

Hi guys
i want to upload file to a server .
i write this function to upload the file to localhost server (iam using wamp server)
private void button1_Click_1(object sender, EventArgs e)
        {
            FileStream fstream = new FileStream(@"C:\Users\Albert\Documents\10050409_3276.doc", FileMode.OpenOrCreate);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/upload_file");
            request.Method = "PUT";
            request.ContentLength = fstream.Length;
            request.AllowWriteStreamBuffering = true;
            Stream request_stream = request.GetRequestStream();
            byte[] indata = new byte[1024];
            int bytes_read = fstream.Read(indata, 0, indata.Length);
            while (bytes_read > 0)
            {
                request_stream.Write(indata, 0, indata.Length);
                bytes_read = fstream.Read(indata, 0, indata.Length);
            }
            fstream.Close();
            request_stream.Close();
            request.GetResponse();
            MessageBox.Show("ok");
        }


so when i click on the button the exception apper said that :
Additional information: The remote server returned an error: (405) Method Not Allowed.

i try to use "POST" instead of "PUT" so the program work and the message box apper to say 'ok' but when i open the localhost->upload_file(folder) i didn't find any files .

so whats the problem ??

thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Upload file to server using Http web request

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Upload file to server using Http web request

Posted 29 August 2011 - 05:09 AM

Uh...the PUT method is not allowed on the webserver? PUT is not generally available out of the box, like GET and POST.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4485
  • View blog
  • Posts: 7,813
  • Joined: 08-June 10

Re: Upload file to server using Http web request

Posted 29 August 2011 - 06:22 AM

Do you have code on the other end of that to accept a file upload in that parameter? You can't just put to any URL and expect it to work.
Was This Post Helpful? 0
  • +
  • -

#4 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

Re: Upload file to server using Http web request

Posted 13 September 2011 - 07:52 AM

any result guys ??

i test my program with wamp server => the problem appear
i test my program with real server and put the network credential and try to upload to folder that have (777) permission => the problem appear

so where is the problem exactly ??

This post has been edited by morsev: 13 September 2011 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 Ändrew  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

Re: Upload file to server using Http web request

Posted 13 September 2011 - 08:39 PM

View Postmorsev, on 14 September 2011 - 01:52 AM, said:

so where is the problem exactly ??


The problem is as Curtis Rutland stated is that you need to either tell us the code on the server side or make some server side code for handling the file you are posting to the web server.

You could do a PHP file for handling the incoming file since you are using WAMP. Here is an example of a PHP upload.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1