11

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

4

3 回答 3

23

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

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
        {
                // ...
        }
    }
}
于 2012-10-19T06:39:07.760 回答
-1

您应该使用 OpenFileDialog,这是一个链接:

http://msdn.microsoft.com/en-us/library/aa984392%28v=vs.71%29.aspx

于 2012-10-19T06:44:18.300 回答
-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);
}
于 2012-10-19T06:05:35.087 回答