gpt4 book ai didi

c# - 使用C#将文件上传到Web服务器

转载 作者:行者123 更新时间:2023-11-30 19:57:13 25 4
gpt4 key购买 nike

我正在尝试使用 C# 在 Web 服务器中上传文件,如下所示

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);
}

但是每次我都会收到这个错误:

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

最佳答案

我已经使用 POST 方法和服务器端代码解决了这个问题:

C#代码

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);
}

服务器端PHP代码 upload.php

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

关于c# - 使用C#将文件上传到Web服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31086117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com