如何使用C#windows窗体将文件上传到Web服务器 [英] How to upload file to web server using C# windows form

查看:82
本文介绍了如何使用C#windows窗体将文件上传到Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,假设我有Windows窗体包含打开的对话框和按钮,以便



uploade file to webserver (http:// localhost :79 / uploade /)我怎么做这个





帮帮我



I need help suppose i have windows form contain open dialog and button ineed to

uploade file to webserver (http://localhost:79/uploade/) how i can do this


help me

please

推荐答案

你需要使用类 System.Net .HttpWebRequest 使用HTTP请求方法POST。



请参阅:

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx [ ^ ] ,

http://msdn.microsoft.com/en -us / library / system.net.webrequest.aspx [ ^ ]。



您可以在此处找到上传示例:http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form -data [ ^ ]。



-SA
You need to use the class System.Net.HttpWebRequest with the HTTP request method "POST".

Please see:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^].

You can find the upload sample here: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data[^].

—SA


您可以上传文件使用WebClient的UploadFile方法。

有关详细信息,请参阅MSDN库。



WebClient类

http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx [ ^ ]



WebClient.UploadFile方法

http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile(VS.80).aspx [ ^ ]



WebClient.UploadFile方法(字符串,字符串)

http://msdn.microsoft.com/en-us/library/36s52zhs(VS.80).aspx [ ^ ]



谢谢
You can up-load the file by using UploadFile Method of WebClient.
Please refer to the MSDN library for details.

WebClient Class
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx[^]

WebClient.UploadFile Method
http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile(VS.80).aspx[^]

WebClient.UploadFile Method (String, String)
http://msdn.microsoft.com/en-us/library/36s52zhs(VS.80).aspx[^]

Thanks


在Windows窗体中:

in windows form:
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/Uploader/");
    {
        client.Headers.Add("fileName", System.IO.Path.GetFileName(fileName));
        client.UploadFileAsync(uri, fileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}



服务器:


in server:

[HttpPost]
public async Task<object> Uploader()
{
    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;
}


这篇关于如何使用C#windows窗体将文件上传到Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆