I wrote an example for uploading files through Web services. If you are free, take a look.

Source: Internet
Author: User
Tags stack trace
I wrote an example for uploading files through Web services. If you are free, take a look.

I wrote an example just now. I tried to use WebService to upload files. You can also paste the example here:

Server code:

The server code implements a simple uploadfile method, which receives byte [] arrays. I have not tried other types. It is estimated that filestream is too difficult to use, but it is easy to use simple types, the second parameter is the relative path to be saved. After the method is executed, the physical file path on the server is returned. it's easy to write. Sorry. just understand the meaning.

 

Using system; using system. web; using system. collections; using system. web. services; using system. web. services. protocols; using system. io; // Summary of uploadservice // [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] public class uploadservice: system. web. services. webService {public uploadservice () {// If the designed component is used, uncomment the following line // initializecomponent ();} [webmethod] Public String uploadfile (byte [] file, string path) {try {string phypath = server. mappath (PATH); filestream FS = new filestream (phypath, filemode. create, fileaccess. write); FS. write (file, 0, file. length); FS. close (); Return phypath;} catch (exception ex) {Throw ex ;}}}

 

 

Client Call Code: (add a web reference to point to the asmx file, such as http: // localhost: 2339/testsite/uploadservice. asmx)

 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace ServiceTest{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button2_Click(object sender, EventArgs e){openFileDialog1.ShowDialog(this);textBox1.Text = openFileDialog1.FileName;}private void button1_Click(object sender, EventArgs e){FileStream fs = File.OpenRead(textBox1.Text);byte[] file = new byte[fs.Length];fs.Read(file, 0, file.Length);localhost.UploadService us = new ServiceTest.localhost.UploadService();textBox2.Text = us.UploadFile(file, Path.GetFileName(textBox1.Text));}}}

There are no special cases to make too many judgments. A WebService is added to the web project, and a winform project is written from the local machine to call the service, note that when uploading large files. modify the configuration as follows: (Note httpruntime)

 

<System. Web> 

Reference address: http://www.openzone.cn/blogs/opennet/archive/2006/07/23/34.aspx

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.