我想将带有C#windows窗体项目的文件上载到Web服务器

我想将带有C#windows窗体项目的文件上载到Web服务器,c#,web-services,visual-studio,C#,Web Services,Visual Studio,我想用windows窗体创建一个C#应用程序,让我可以将文件上传到Web服务器,我看过很多教程,它们都证明对解决我的问题毫无用处 对于我的项目,我在按钮中有下一个上传代码 WebClient client = new WebClient(); client.UploadFile("http://localhost:8080/", location); 从这里我有几个错误,一个尝试多个想法,我的一些更常见的错误是404找不到或不可访问的路径,有时它不会向我显示错误并工作,但文件

我想用windows窗体创建一个C#应用程序,让我可以将文件上传到Web服务器,我看过很多教程,它们都证明对解决我的问题毫无用处

对于我的项目,我在按钮中有下一个上传代码

 WebClient client = new WebClient();
        client.UploadFile("http://localhost:8080/", location);
从这里我有几个错误,一个尝试多个想法,我的一些更常见的错误是404找不到或不可访问的路径,有时它不会向我显示错误并工作,但文件没有保存在指定的路径中

下面是我用来解决问题的一些链接:

winform

string fullUploadFilePath = @"C:\Users\cc\Desktop\files\test.txt";
string uploadWebUrl = "http://localhost:8080/upload.aspx";
client.UploadFile(uploadWebUrl , fullUploadFilePath );
asp.net创建
upload.aspx
如下

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {

    foreach(string f in Request.Files.AllKeys) {
        HttpPostedFile file = Request.Files[f];
        file.SaveAs(Server.MapPath("~/Uploads/" + file.FileName));
    }   
}

</Script>
<html>
<body>
<p> Upload complete.  </p>
</body>
</html>

无效页面加载(对象发送方,事件参数e){
foreach(Request.Files.AllKeys中的字符串f){
HttpPostedFile file=Request.Files[f];
SaveAs(Server.MapPath(“~/Uploads/”+file.FileName));
}   
}
上传完成


使用C#从本地硬盘将文件上载到FTP服务器

private void UploadFileToFTP()
{
   FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create("ftp://www.server.com/sample.txt");

   ftpReq.UseBinary = true;
   ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
   ftpReq.Credentials = new NetworkCredential("user", "pass");

   byte[] b = File.ReadAllBytes(@"E:\sample.txt");
   ftpReq.ContentLength = b.Length;
   using (Stream s = ftpReq.GetRequestStream())
   {
        s.Write(b, 0, b.Length);
   }

   FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

   if (ftpResp != null)
   {
         if(ftpResp.StatusDescription.StartsWith("226"))
         {
              Console.WriteLine("File Uploaded.");
         }
   }
}
在windows中:

private void uploadButton_Click(object sender, EventArgs e)
{
    var openFileDialog = new OpenFileDialog();
    var dialogResult = openFileDialog.ShowDialog();    
    if (dialogResult != DialogResult.OK) return;              
    Upload(openFileDialog.FileName);
}

private void Upload(string fileName)
{
    var client = new WebClient();
    var uri = new Uri("http://www.yoursite.com/UploadMethod/");  
    try
    {
        client.Headers.Add("fileName", System.IO.Path.GetFileName(fileName));
        var data = System.IO.File.ReadAllBytes(fileName);
        client.UploadDataAsync(uri, data);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
在服务器中:

[HttpPost]
public async Task<object> UploadMethod()
{
    var file = await Request.Content.ReadAsByteArrayAsync();
    var fileName = Request.Headers.GetValues("fileName").FirstOrDefault();
    var filePath = "/upload/files/";
    try
    {
        File.WriteAllBytes(HttpContext.Current.Server.MapPath(filePath) + fileName, file);           
    }
    catch (Exception ex)
    {
        // ignored
    }

    return null;
}
[HttpPost]
公共异步任务上载方法()
{
var file=wait Request.Content.ReadAsByteArrayAsync();
var fileName=Request.Headers.GetValues(“fileName”).FirstOrDefault();
var filePath=“/upload/files/”;
尝试
{
File.writealBytes(HttpContext.Current.Server.MapPath(filePath)+文件名,文件);
}
捕获(例外情况除外)
{
//忽略
}
返回null;
}

在WinForms端,您所显示的代码实际上就是您所需要的全部。。。你还需要一个需要上传的服务器、正确的文件位置和正确的目标url,但你的帖子中遇到的错误类型还不清楚。如果我使用该代码,它实际上运行程序,但它不会将文件保存在指定的位置,我试图将其更改为C:\但情况与您告诉我的参数相同,我将尝试使用这些参数,看看是否得到一些结果,谢谢。您为什么使用ASP.net??有关我的问题的更多内容,我可以在没有asp.net的情况下下载,并且我不想使用asp.net进行上载,只有在确实需要解决我的问题+1的情况下@user3353954-您需要一些东西,可以在
localhost:8080
上接受POST HTTP请求。ASP.Net在C#community中广泛使用HTTP堆栈(与.Net framework/Windows一起提供),因此您将看到的大多数HTTP处理示例都使用ASP.Net。当然,您可以使用任何其他HTTP服务器-在服务器端,通过
上传文件发送的POST请求中绝对没有ASP.Net特定内容,我想在我的项目中添加代码。