使用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. ...
随机推荐
- c语言程序设计:用strcpy比较数组(银行卡密码程序设计),strcpy(复制数组内容)和getchar()(敲键盘字符,统计不想要的字符的个数)
统计从键盘输入一行字符的个数: 1 //用了getchar() 语句 2 //这里的\n表示回车 #include <stdio.h> #include <stdlib.h> ...
- 关于Json.dumos中的ensure_ascii
在使用json,dumps时,当需要输出中文时,需要在后面添加 ensure_ascii = Fasle 因为json.dumps 序列化时,默认对中文使用的是 ascii 编码,添加后才能输出中文 ...
- BZOJ1800_fly飞行棋_KEY
题目传送门 看数据范围,N<=20! 你没看错,搜索都能过. O(N^2)的做法,就是先求出有几对点之间的距离为圆周长的一半. 然后求C(N,2)即可. code: /************* ...
- 北京Uber优步司机奖励政策(2月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- PHP程序员如何理解依赖注入容器(dependency injection container)
背景知识 传统的思路是应用程序用到一个Foo类,就会创建Foo类并调用Foo类的方法,假如这个方法内需要一个Bar类,就会创建Bar类并调用Bar类的方法,而这个方法内需要一个Bim类,就会创建Bim ...
- c++ reference can not be reassigned
#include <iostream> using namespace std; int main () { // declare simple variables int i; int ...
- HardcodedDebugMode
xmlns:tools="http://schemas.android.com/tools" tools:ignore="HardcodedDebugMode"
- Android性能优化来龙去脉总结
WeTest 导读 一款app除了要有令人惊叹的功能和令人发指交互之外,在性能上也应该追求丝滑的要求,这样才能更好地提高用户体验. 以下是本人在工作中对经历过的性能优化的一些总结,依据故事的发展路线, ...
- 「LeetCode」0001-Two Sum(Ruby)
题意与分析 题意直接给出来了:给定一个数,返回数组中和为该数(下为\(x\))的两个数的下标. 这里有一个显然的\(O(n)\)的实现:建立一个hash表,每次读入数(记作\(p\))的时候查询has ...
- hdu5698瞬间移动(杨辉三角+快速幂+逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...