File upload to web server using C#

upload file via web service c#
how to upload file from client to server in asp.net c#
upload file to server c# windows application
upload file using httpwebrequest c#
file upload using web method in asp.net c#
web api file upload
file upload in c# windows application
c# post file to web api

I am trying to upload file in web server as following using C#

try
{
    // create WebClient object
    WebClient client = new WebClient();

    string myFile = @"D:\test_file.txt";
    client.Credentials = CredentialCache.DefaultCredentials;

    // client.UploadFile(@"http://mywebserver/myFile", "PUT", myFile);
    client.UploadFile(@"http://localhost/uploads", "PUT", myFile);
    client.Dispose();
}
catch (Exception err)
{
    MessageBox.Show(err.Message);
}

But every time I am getting this error:

The remote server returned an error: (405) Method Not Allowed.

I have solved this using the POST method and server side code:

C# code

try
{
    WebClient client = new WebClient();
    string myFile = @"D:\test_file.txt";
    client.Credentials = CredentialCache.DefaultCredentials;
    client.UploadFile(@"http://localhost/uploads/upload.php", "POST", myFile);
    client.Dispose();
}
catch (Exception err)
{
    MessageBox.Show(err.Message);
}

Server side PHP code upload.php

<?php
    $filepath = $_FILES["file"]["tmp_name"];
    move_uploaded_file($filepath,"test_file.txt");
?>

File upload to web server using C#, You need to use the class System.Net.HttpWebRequest with the HTTP request method "POST". Please see: This step-by-step article describes how to upload a file to a Web server by using Visual C# .NET. In this article, you create a Microsoft ASP.NET file (WebForm1.aspx) and its related code-behind file (WebForm1.aspx.cs) to upload files to a directory that is named Data. back to the top Create an ASP.NET application

The error means that the "PUT" method you are using is not allowed by the server. Check the response headers for allowed methods. More info here.

Or check the documentation for the application to which you are trying to upload the file.

How to upload file to web server using C# windows form, /// file onto the server; the UploadFile web method; /// will accept the report and store it in the local file system. /// <  Is there need server side code?. I thaught file will be uploaded to desired folder in server. If needed what will be server side code please. – MaxEcho Jun 27 '15 at 7:07

the error is showing that you need to register with the service which you are using

in the case of wcf you can register like this

"%WINDIR%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r

HTTP Error 405 Method not allowed

Upload any type of File through a C# Web Service, How upload file to server with HTTP POST multipart form data? The test application contains a single Windows Form class; this form contains a text box used to display the name of the file selected for upload, a browse button used to launch an open file dialog box which is used to navigate to and select a file for upload, and an upload button which is used to pass the file to web service so that the selected file may be stored on the server.

How to upload files to the Web server with FileUpload control in C# , ASP.NET includes a control named FileUpload that allows website users to upload files to the web server. The FileUpload control manages the posted file data,  When address specifies an HTTP resource, the UploadFile method sends a local file to a resource using the HTTP method specified in the method parameter and returns any response from the server. This method blocks while uploading the file. To continue executing while waiting for the server's response, use one of the UploadFileAsync methods.

C# Upload Files to a Web Server, This is the simplest example for uploading some files to a web server. the BlockingUpload call is synchronous and returns when the upload is finished (or failed). The SaveAs method saves the contents of an uploaded file to a specified path on the Web server. The FileUpload control does not automatically save a file to the server after the user selects the file to upload. You must explicitly provide a control or mechanism to allow the user to submit the specified file.

C# Tutorial, C# Uploading Files to a Webserver Tutorial - Intermediate C# FTP Uploader Visual C# How to Duration: 9:55 Posted: May 16, 2016 I try upload a file to an FTP-server with C#. The file is uploaded but with zero bytes. private void button2_Click(object sender, EventArgs e) { var dirPath = @"C:/Documents and Settings/sande

Comments
  • What is your server code? Is it an MVC action?
  • Are you sure you need a PUT could it be a POST you need to send?
  • Is there need server side code?. I thaught file will be uploaded to desired folder in server. If needed what will be server side code please.
  • The error message says that PUT method is not allowed by the given server. It requires you code for the server, you can create an MVC application with a controller, a put method.