c# winform通过webservice下载文件

来源:互联网 发布:mysql 5.6安装win7 编辑:程序博客网 时间:2024/06/07 13:11

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 winform{    public partial class Form1 : Form    {        private ws.WebService1 lws = new winform.ws.WebService1();        public Form1()        {            InitializeComponent();            this.Load+=new EventHandler(Form1_Load);        }        private void Form1_Load(object sender, EventArgs e)        {            DownloadImage();        }        /// <summary>        /// 下载图片附件        /// </summary>        /// <param name="attachments"></param>        /// <returns></returns>        private void DownloadImage()        {            string path = @"image\img2.jpg";//服务器图片路径            string directoryPath = @"c:\test";//本地文件夹路径            string downloadPath = @"c:\test\img2.jpg";//本地图片路径            try            {                byte[] bytes = lws.DownloadFile(path);                if (bytes != null)                {                    if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); }//如果不存在完整的上传路径就创建                    FileInfo downloadInfo = new FileInfo(directoryPath);                    if (downloadInfo.IsReadOnly) { downloadInfo.IsReadOnly = false; }                    //定义并实例化一个内存流,以存放提交上来的字节数组。                    MemoryStream ms = new MemoryStream(bytes);                    //定义实际文件对象,保存上载的文件。                    FileStream fs = new FileStream(downloadPath, FileMode.Create);                    ///把内内存里的数据写入物理文件                    ms.WriteTo(fs);                    fs.Flush();                    ms.Flush();                    ms.Close();                    fs.Close();                    fs = null;                    ms = null;                }            }            catch            {            }        }    }}

2)webservice

using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Linq;using System.IO;namespace web{    /// <summary>    /// WebService1 的摘要说明    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    [ToolboxItem(false)]    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。    // [System.Web.Script.Services.ScriptService]    public class WebService1 : System.Web.Services.WebService    {        [WebMethod]        public string HelloWorld()        {            return "Hello World";        }        /// <summary>        /// 下载文件        /// </summary>        /// <param name="filePath">文件路径</param>        /// <returns>返回文件流</returns>        [WebMethod]        public byte[] DownloadFile(string path)        {            FileStream fs = null;            path = Server.MapPath(path);            if (File.Exists(path))            {                try                {                    ///打开现有文件以进行读取。                    fs = File.OpenRead(path);                    return ConvertStreamToByteBuffer(fs);                }                catch (Exception ex)                {                    return new byte[0];                }                finally                {                    fs.Close();                }            }            else            {                return new byte[0];            }        }        /// <summary>        /// 把给定的文件流转换为二进制字节数组。        /// </summary>        /// <param name="stream">文件流</param>        /// <returns>返回二进制数组</returns>        public static byte[] ConvertStreamToByteBuffer(System.IO.Stream stream)        {            int b1;            System.IO.MemoryStream tempStream = new System.IO.MemoryStream();            while ((b1 = stream.ReadByte()) != -1)            {                tempStream.WriteByte(((byte)b1));            }            return tempStream.ToArray();        }    }}


 

原创粉丝点击