架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 44500|回复: 9

[WCF/Web Servics] WCF实现文件上传有返回值Demo下载

[复制链接]
发表于 2016-7-20 11:36:54 | 显示全部楼层 |阅读模式
QQ截图20160720112134.jpg
项目,利用wcf实现文件上传,并且返回上传的结果,测试了一个30M的大文件,也能上传成功。

客户端主要代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.IO;
  9. using System.Windows.Forms;

  10. namespace WCFclient
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }

  18.         private void button1_Click(object sender, EventArgs e)
  19.         {
  20.             using (OpenFileDialog open = new OpenFileDialog())
  21.             {
  22.                 open.Multiselect = false;
  23.                 open.Title = "文件上传";
  24.                 open.Filter = "文件|*.jpg;*.png;*.gif;*.jpeg;*.bmp;*.exe;*.zip;*.rar";
  25.                 if (open.ShowDialog() == DialogResult.OK)
  26.                 {
  27.                     string name = System.IO.Path.GetFileName(open.FileName);
  28.                     ServiceDemo.Service1Client client = new ServiceDemo.Service1Client();
  29.                     using (FileStream str = new FileStream(open.FileName, FileMode.Open, FileAccess.Read))
  30.                     {
  31.                         bool result;
  32.                         string msg = client.Upload(name, str, out result);
  33.                         client.Close();
  34.                         label1.Text = msg;
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }
复制代码


WCF服务主要代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. using System.IO;

  9. namespace WcfServiceDemo
  10. {
  11.     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
  12.     // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。
  13.     public class Service1 : IService1
  14.     {
  15.         public string GetData(int value)
  16.         {
  17.             return string.Format("You entered: {0}", value);
  18.         }

  19.         public CompositeType GetDataUsingDataContract(CompositeType composite)
  20.         {
  21.             if (composite == null)
  22.             {
  23.                 throw new ArgumentNullException("composite");
  24.             }
  25.             if (composite.BoolValue)
  26.             {
  27.                 composite.StringValue += "Suffix";
  28.             }
  29.             return composite;
  30.         }


  31.         public ResultMessage Upload(FileModel file)
  32.         {
  33.             if (file.Name == null || file.stream == null)
  34.             {
  35.                 ResultMessage msg = new ResultMessage() { ErrorMessage = "上传失败!", IsSuccessed = false };
  36.                 return msg;
  37.             }

  38.             string path = AppDomain.CurrentDomain.BaseDirectory + @"" + file.Name;

  39.             #region 接受文件方法1
  40.             try
  41.             {
  42.                 using (FileStream outputStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
  43.                 {
  44.                     // 我们不用对两个流对象进行读写,只要复制流就OK  
  45.                     file.stream.CopyTo(outputStream);
  46.                     outputStream.Flush();
  47.                     ResultMessage msg = new ResultMessage() { ErrorMessage = "上传成功!" + path, IsSuccessed = true };
  48.                     return msg;
  49.                 }
  50.             }
  51.             catch (Exception ex)
  52.             {
  53.                 ResultMessage msg = new ResultMessage() { ErrorMessage = "上传失败!" + ex.Message, IsSuccessed = false };
  54.                 return msg;
  55.             }
  56.             #endregion
  57.             

  58.             #region 接受文件方法2
  59.             FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
  60.             try
  61.             {

  62.                 BinaryReader reader = new BinaryReader(file.stream);
  63.                 byte[] buffer;
  64.                 BinaryWriter writer = new BinaryWriter(fs);
  65.                 long offset = fs.Length;
  66.                 writer.Seek((int)offset, SeekOrigin.Begin);
  67.                 do
  68.                 {
  69.                     buffer = reader.ReadBytes(1024);
  70.                     writer.Write(buffer);

  71.                 } while (buffer.Length > 0);

  72.                 fs.Close();
  73.                 file.stream.Close();
  74.                 ResultMessage msg = new ResultMessage() { ErrorMessage = "上传成功!" + path, IsSuccessed = true };
  75.                 return msg;
  76.             }
  77.             catch (Exception e)
  78.             {
  79.                 ResultMessage msg = new ResultMessage() { ErrorMessage = "上传失败!" + e.Message, IsSuccessed = false };
  80.                 return msg;
  81.             }
  82.             finally
  83.             {
  84.                 fs.Close();
  85.                 file.stream.Close();

  86.             }
  87.             #endregion
  88.             
  89.         }
  90.     }
  91. }
复制代码


客户端和服务端代码demo下载:

游客,如果您要查看本帖隐藏内容请回复







上一篇:The operation 'xxx' could not be loaded because it has a parameter or retu...
下一篇:There was no endpoint listening at http://localhost:111/xxx.svc that c...
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-11-8 20:46:20 | 显示全部楼层
Mark!!!先赞一个
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-12-3 10:18:28 | 显示全部楼层
很实用!
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-12-9 09:23:32 | 显示全部楼层
正是需要的
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2018-6-9 08:20:19 | 显示全部楼层
12323234545
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2018-7-14 17:55:22 | 显示全部楼层
哈哈,我看看先
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2018-8-3 17:05:42 | 显示全部楼层
感谢楼主
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2020-10-12 15:24:20 | 显示全部楼层
先收起来,感谢
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2020-10-12 17:40:17 | 显示全部楼层
学习,感谢分享!
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2020-11-3 14:32:22 | 显示全部楼层
我看看先
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-8-5 07:05

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表