c# winform通过webservice 上传文件

1)winform 窗体

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private ws.Service1 lws = new WindowsFormsApplication1.ws.Service1();
        public Form1()
        {
            InitializeComponent();
            this.Load+=new EventHandler(Form1_Load);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            UploadImage();
        }
        /// <summary>
        /// 上传图片附件
        /// </summary>
        /// <param name="Attachments"></param>
        private bool UploadImage()
        {
            bool flag = true;
            string path = @"C:\Documents and Settings\Administrator\My Documents\Wallpaper\img18.jpg";//本地路径
            byte[] bytes = GetBytesByPath(path);//获取文件byte[]
            string uploadPath = "image";//上传服务器文件夹路径
            string fileName = "img18.jpg";//文件名称
            try
            {
                if (lws.UploadFile(bytes, uploadPath, fileName)) { flag = true; }
                else { flag = false; }
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
/// <summary>
        /// 根据文件的路径获取图片的byte[]
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static byte[] GetBytesByPath(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((int)fs.Length);
            fs.Flush();
            fs.Close();
            return bytes;
        }
    }
}

2)webservice

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
namespace WebService1
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fs">文件的byte[]</param>
        /// <param name="path">上传文件的路径</param>
        /// <param name="fileName">上传文件名字</param>
        /// <returns></returns>
        [WebMethod]
        public bool UploadFile(byte[] fs, string path, string fileName)
        {
            bool flag = false;
            try
            {
                //获取上传案例图片路径
                path = Server.MapPath(path);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //定义并实例化一个内存流,以存放提交上来的字节数组。
                MemoryStream m = new MemoryStream(fs);
                //定义实际文件对象,保存上载的文件。
                FileStream f = new FileStream(path + "\\" + fileName, FileMode.Create);
                //把内内存里的数据写入物理文件
                m.WriteTo(f);
                m.Close();
                f.Close();
                f = null;
                m = null;
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值