How to upload images in WinForm

Source: Internet
Author: User

Http://jingyan.baidu.com/article/b7001fe157d6b60e7382dd7f.html

Because WinForm are all running locally, and our site is generally on the server, running on the server, so upload files on the site, it is like saving files to local. But it's not the same on WinForm, this chapter we simply cite a way to upload files to the server in WinForm using WebService

First, start by creating a WebService service that contains a Updatefile method that receives two byte[] and string type parameters. The method is very simple, which is to save the byte[] parameter value to the server by the path and name specified by the string parameter, and the code is as follows:

[WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)] public class WebService:System.Web.Services.WebService {[WebMethod] public bool Updatefile (byte[] content, string pathandname) {file.writeallbytes (Server.MapPath (pathandname), content);}}

For security, we can verify the value of the pathandname so that it only saves files in the image format. The full code is as follows:

[WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)] public class WebService:System.Web.Services.WebService {[WebMethod] public bool Updatefile (byte[] content, string pathandname) {int index = Pathandname. LastIndexOf ("."); if (index = = 0) {return false;} else {string extended = string. Empty; if (index + 1 = = Pathandname. Length) {return false;} else {extended = Pathandname. Substring (index + 1); if (extended = = "JPEG" | | extended = = "GIF" | | extended = = "jpg") {try {file.writeallbytes (Server.MapPath (Pathandname), content); return true; } catch (Exception ex) {return false;}} else {return false;}} } } }

Okay, after you've created WebService, put it on the server, and then add a reference to the service in WinForm, adding the following method:
In the WinForm project reference-Add a service reference, add a good webservice address in the address bar of the Open dialog box, click Go, verify that the add succeeds after passing. Such as:

Then, we can use it in the application, for security, we re-verify in WinForm that the upload file can only be a picture. The code is as follows:

private void Button11_click (object sender, EventArgs e) {OpenFileDialog FileDialog = new OpenFileDialog ();

if (filedialog.showdialog () = = DialogResult.OK) {string extension = Path.getextension (filedialog.filename); string[] str = new string[] {". gif", ". Jpge", ". jpg"}; if (!str. Contains (extension)) {MessageBox.Show ("only images in gif,jpge,jpg format can be uploaded! "); } else {FileInfo FileInfo = new FileInfo (filedialog.filename), if (Fileinfo.length > 20480) {MessageBox.Show ("The uploaded picture cannot be greater than 20K "); } else {Stream file = Filedialog.openfile ();

byte[] bytes = new Byte[file. Length]; File. Read (bytes, 0, bytes. Length);

Instantiate the WebService service. ServiceReference1 is the namespace we set when we add a reference servicereference1.webservicesoapclient webservicesoapclient = new AutoPage.ServiceReference1.WebServiceSoapClient ();

DateTime time = DateTime.Now; Rename the name of the picture with the path string pathandname = "/images/" + time. ToString ("YYYYMMDDHHMMSS") + extension; if (Webservicesoapclient.updatefile (bytes, pathandname)) {MessageBox.Show ("Upload succeeded! "); This.textBox1.Text = Pathandname; } else {MessageBox.Show ("Upload failed! "); } } } } }

How to upload images in WinForm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.