File upload to web server using C#

只谈情不闲聊 提交于 2019-12-02 00:34:15
Salim

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");
?>

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!