如何以窗口形式上传文件?

Ssa*_*har 10 winforms

在窗口表单中,如何上传文件,我没有找到任何文件上传控件.你能给我一些参考吗?我想将文档存储在我的系统驱动器中.谢谢.

Ead*_*del 21

您可以使用以下代码放置表单按钮并为其创建单击处理程序:

private void buttonGetFile_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "Text files | *.txt"; // file types, that will be allowed to upload
    dialog.Multiselect = false; // allow/deny user to upload more than one file at a time
    if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK
    {
        String path = dialog.FileName; // get name of file
        using (StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open), new UTF8Encoding())) // do anything you want, e.g. read it
        {
                // ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


jon*_*ham -2

请参阅本教程了解原始 HTTP POST:

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

参考.NET的WebClient类:

http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.80).aspx

一个简单的 HTTP POST 可以这样完成:

string Upload_File_Content = ...;
string Url = ...;

using (var Http_Client = new WebClient()) {
  var Post_Data = new NameValueCollection();
  Post_Data["upload_file"] = Upload_File_Content;

  var Response = Http_Client.UploadValues(Url,"POST",Post_Data);
}
Run Code Online (Sandbox Code Playgroud)