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(); } }}
- c# winform通过webservice下载文件
- c# winform通过webservice下载文件
- c# winform通过webservice 上传文件
- c# winform通过webservice 上传文件
- c# winform 通过web服务下载文件
- C# WinForm 下载文件示例
- c# winform通过本地WebService接口访问SQL数据库实例
- 通过WebService上传和下载文件
- 通过WebService 上传和下载文件
- C# Winform下载文件并显示进度条
- C# Winform下载文件并显示进度条
- c#通过ftp下载文件
- c#通过ftp下载文件
- c# winform 调用WebService实例
- C# WinForm通过WebClient实现文件上传下载 (附源码)
- C# WinForm通过WebClient实现文件上传下载 (附源码)
- 通过Java WebService接口从服务端下载文件
- 通过cab文件下载C# ActiveX组件
- 浅谈简单工作流设计——责任链模式配合策略与命令模式的实现
- make
- ASP.NET误人子弟教程:在MVC下如何返回图片
- Python wxPython-demo-2.8.4.0.tar.bz2
- 为什么成为优秀程序员这么难
- c# winform通过webservice下载文件
- c# winform通过webservice 上传文件
- grep
- FPGA组成、工作原理和开发流程
- 送世界一个新长安——西安向国际化大都市目标迈进
- mysql添加用户、更改密码
- Android: Using monkey from Java(三)
- ADO Oracle错误:“数据提供程序或其他服务返回 E_FAIL 状态”
- 记录我的新工作(十四)