How to upload a file in window form?

In window forms, how to upload a file, I did not find any file upload control. Can you give me a link? I want to save the document in my system drive. Thanks.

+7
source share
3 answers

You can put on your form button and create a click handler for it using the following code:

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, eg read it { // ... } } } 
+15
source

You should use OpenFileDialog, here is the link:

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

0
source

Refer to this guide for raw HTTP POST:

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

Link to the WebClient.NET class:

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

A simple HTTP POST can be done as follows:

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

All Articles