使用WCF上传数据
通过传递Stream对象来传递大数据文件,但是有一些限制:
1、只有 BasicHttpBinding、NetTcpBinding 和 NetNamedPipeBinding 支持传送流数据。
2、 流数据类型必须是可序列化的 Stream 或 MemoryStream。
3、 传递时消息体(Message Body)中不能包含其他数据。
4、TransferMode的限制和MaxReceivedMessageSize的限制等。
下面具体实现:新建一个FileService,接口文件的代码如下:
using System.IO;
using System.ServiceModel; namespace FileService
{ [ServiceContract]
public interface IFileService
{
[OperationContract]
void UpLoadFile(FileUploadMessage fileUploadMessage);
} [MessageContract]
public class FileUploadMessage
{
[MessageHeader]
public string FileName
{
get;
set;
} [MessageBodyMember]
public Stream FileData
{
get;
set;
}
}
}
定义FileUploadMessage类的目的是因为第三个限制,要不然文件名及其他信息就没办法传递给WCF了,根据第二个限制,文件数据是用System.IO.Stream来传递的。下面是接口的具体实现。
using System;
using System.IO; namespace FileService
{
public class FileService : IFileService
{
//保存文件的目录路径
private const string SaveDirectory = @"F:\V2.4ShareFolder\PRO"; public void UpLoadFile(FileUploadMessage fileUploadMessage)
{
string fileName = fileUploadMessage.FileName; Stream sourceStream = fileUploadMessage.FileData; FileStream targetStream = null; if (!sourceStream.CanRead)
{
throw new Exception("数据流不可读!");
} string filePath = Path.Combine(SaveDirectory, fileName); using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
//read from the input stream in 4K chunks
//and save to output stream
const int bufferLen = ; var buffer = new byte[bufferLen]; try
{
int count; while ((count = sourceStream.Read(buffer, , bufferLen)) > )
{
targetStream.Write(buffer, , count);
}
}
finally
{
sourceStream.Close();
}
}
}
}
}
实现的功能很简单,把文件保存到特定的目录中即可。下面是进行config的配置。
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="FileServiceBinding" maxReceivedMessageSize="" transferMode="Streamed"></binding>
</basicHttpBinding>
</bindings>
<services>
<service name="FileService.FileService" behaviorConfiguration="FileServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9090/FileService"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="FileService.IFileService" bindingConfiguration="FileServiceBinding"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="FileServiceBehavior">
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>
新建客户端,进行服务的测试。
namespace TestClient
{
class Program
{
static void Main(string[] args)
{
FileServiceClient fileServiceClient = new FileServiceClient(); string filepath = @"F:\V2.4ShareFolder\EPHMDY1031975917464876001-7131898.zip"; using (Stream stream = new FileStream(filepath, FileMode.Open))
{
fileServiceClient.UpLoadFile("222.zip", stream);
} Console.ReadKey();
}
}
}
使用WCF上传数据的更多相关文章
- 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件
[源码下载] 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...
- TortoiseGit和msysGit安装及使用笔记(windows下使用上传数据到GitHub)[转]
TortoiseGit和msysGit安装及使用笔记(windows下使用上传数据到GitHub) Git-1.7.11-preview+GitExtensions244SetupComplete+T ...
- Amzon MWS API开发之 上传数据
亚马逊上传数据,现有能操作的功能有很多:库存数量.跟踪号.价格.商品....... 我们可以设置FeedType值,根据需要,再上传对应的xml文件即可. 下面可以看看FeedType类型 这次我们拿 ...
- Amazon MWS 上传数据 (三) 提交请求
前面介绍了设置服务和构造请求,现在介绍提交请求. 上传数据,查询上传操作的工作状态,和处理上传操作返回的报告操作使用的Amazon API 分别为:SubmitFeed(),FeedSubmissio ...
- Amazon MWS 上传数据 (二) 构造请求
上一篇文章提到了Amazon 上传数据有三个步骤,但是每个步骤都需要构造服务和构造请求,服务是一样的,请求各不相同:这个很容易理解,这三个步骤都需要和Amazon服务器交互,所以他们的服务构造是一样的 ...
- Amazon MWS 上传数据 (一) 设置服务
Amazon 上传数据的流程为: 通过 SubmitFeed 操作.加密标头和所有必需的元数据(包括 FeedType 的值在内),来提交 XML 或文本型数据文件.正如亚马逊 MWS的所有提交内容一 ...
- 说说ajax上传数据和接收数据
我是一个脑袋不太灵光的人,所以遇到问题,厚着脸皮去请教大神的时候,害怕被大神鄙视,但是还是被鄙视了.我说自己不要点脸面,那是不可能的,但是,为了能让自己的技术生涯能走的更长远一些,受点白眼,受点嘲笑也 ...
- webclient上传数据到ashx服务
1.上传参数 UploadData()方法可以上传数据参数,需要将所要上传的数据拼成字符. // 创建一个新的 WebClient 实例. WebClient myWebClient = new ...
- ueditor富文本上传图片的时候报错"未找上传数据"
最近因为需求所以在ssh项目中使用了Ueditor富文本插件,但是在上传图片的时候总是提示“未找到上传数据”,之后百度了好久终于弄明白了.因为Ueditor在上传图片的时候会访问controller. ...
随机推荐
- ACM1003:Max Sum
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- for循环练习题(1 ,判断任意一个数是91的多少倍 2,编写程序实现给定一个整数判断它从0到这个整数中间出现多少次9的次数)
1 //判断任意一个数是9的多少倍 #include <stdio.h> #include <stdlib.h> int main() { printf("请输入任意 ...
- jz_2440_电阻屏触摸驱动
驱动中: 入口函数init内: /* 1. 分配一个input_dev结构体 */ s3c_ts_dev = input_allocate_device(); /*------------------ ...
- 使用 Vim 搭建 JavaScript 开发环境
原文链接: https://spacevim.org/cn/use-vim-as-a-javascript-ide/ SpaceVim 是一个模块化的 Vim IDE,针对 JavaScript 这一 ...
- 使用ChipScope Pro调试硬件
chipscope_icon提供与其他ChipScope内核的通信 chipscope_opb_iba促进传统片上外设总线(OPB)事务的监控 chipscope_plb_iba便于监控处理器本地总线 ...
- Android开发——Context类的各种细节问题
0. 前言 Context相信所有的Android开发人员基本上每天都在接触,因为它太常见了.但实际上Context有太多小的细节并不被大家所关注,那么今天我们就来学习一下那些你所不知道的细节. ...
- OpenCV代码提取:dft函数的实现
The Fourier Transform will decompose an image into its sinus and cosines components. In other words, ...
- android学习十三 首选项
1,首选项可用用来持久保存用户设置,游戏最高分等 2,首选项有,列表首选项,复选框首选项,对话框首选项.. 3,通过xml文件和代码创建首选项 addPreferencesFromResou ...
- 那些年我们不爱学的mysql单词
MySQL 一种关系型数据库 database 数据库,简称DB databases 数据库的复数,代表多个数据库 net 网络/服务 start 启动 stop 停止 root MySQL数据库中的 ...
- SQL Sever查询语句集锦
一. 简单查询简单的Transact-SQL查询只包括选择列表.FROM子句和WHERE子句.它们分别说明所查询列.查询的表或视图.以及搜索条件等. 例如,下面的语句查询testtable表中姓名为“ ...