WCF中的Stream操作有很多使用方法,其中有一种比较常用的就是我们今天为大家介绍的关于实现上传大文件的操作方法。在这里我们就会通过这篇文章为大家详细介绍一下相关的操作方法。
潞州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
WCF Stream操作步骤之Test.ASPX.C
- protected void Button3_Click(object sender, EventArgs e)
- {
- FileData file = new FileData();
- file.filename = FileUpload1.FileName;
- file.data = new FileStream(FileUpload1.PostedFile.
FileName, FileMode.Open);- GetDataServiceClient c = new GetDataServiceClient();
- c.UploadFile(file.filename, file.data);
- Response.Write("文件传输成功!");
- c.Close();
- }
WCF Stream操作步骤之Contract
- [ServiceContract]
- public interface IGetDataService
- {
- [OperationContract]
- void UploadFile(FileData file);
- }
- [MessageContract]
- public class FileData
- {
- [MessageHeader]
- public string filename;
- [MessageBodyMember]
- public Stream data;
- }
WCF Stream操作步骤之ServiceLib
- public class GetDataService : IGetDataService
- {
- public void UploadFile(FileData file)
- {
- FileStream fs = new FileStream("Files\\"+file.filename,
FileMode.OpenOrCreate);- try
- {
- BinaryReader reader = new BinaryReader(file.data);
- byte[] buffer;
- BinaryWriter writer = new BinaryWriter(fs);
- long offset = fs.Length;
- writer.Seek((int)offset, SeekOrigin.Begin);
- do
- {
- buffer = reader.ReadBytes(1024);
- writer.Write(buffer);
- } while (buffer.Length > 0);
- }
- catch(Exception e)
- {
- }
- finally
- {
- fs.Close();
- file.data.Close();
- }
- }
- }
WCF Stream操作步骤之App.config
- < ?xml version="1.0" encoding="utf-8" ?>
- < configuration>
- < system.serviceModel>
- < services>
- < !--name - 提供服务的类名-->
- < !--behaviorConfiguration - 指定相关的行为配置-->
- < service name="ServiceLib.GetDataService"
behaviorConfiguration="BindingBehavior">- < !--address - 服务地址-->
- < !--binding - 通信方式-->
- < !--contract - 服务契约-->
- < !--< endpoint binding="basicHttpBinding" contract=
"WCF.ServiceLib.Binding.IHello" address="Hello" />-->- < !--元数据交换的endpoint-->
- < !--注:address是mex,它会和host/baseAddresses节点中的baseAddress做拼接,
即提供元数据交换的地址为:http://localhost:12345/Binding/mex-->- < endpoint binding="basicHttpBinding" bindingConfiguration =
"DocumentExplorerServiceBinding" contract="Contract.IGetDataService"
address="mex" />- < host>
- < baseAddresses>
- < add baseAddress="http://localhost:8008/"/>
- < /baseAddresses>
- < /host>
- < /service>
- < /services>
- < behaviors>
- < serviceBehaviors>
- < behavior name="BindingBehavior">
- < !--httpGetEnabled - 使用get方式提供服务-->
- < serviceMetadata httpGetEnabled="true" />
- < /behavior>
- < /serviceBehaviors>
- < /behaviors>
- < bindings>
- < basicHttpBinding>
- < binding name="DocumentExplorerServiceBinding"
- sendTimeout="00:10:00"
- transferMode="Streamed"
- maxReceivedMessageSize="9223372036854775807">
- < /binding>
- < /basicHttpBinding>
- < /bindings>
- < /system.serviceModel>
- < /configuration>
WCF Stream操作步骤之web.config
- < system.serviceModel>
- < bindings>
- < basicHttpBinding>
- < binding name="BasicHttpBinding_IGetDataService" closeTimeout="00:01:00"
- openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
- allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"- maxBufferSize="65536" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"- transferMode="Streamed"
- useDefaultWebProxy="true">
- < readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"- maxBytesPerRead="4096" maxNameTableCharCount="16384" />
- < security mode="None">
- < transport clientCredentialType="None" proxyCredentialType="None"
- realm="" />
- < message clientCredentialType="UserName" algorithmSuite="Default" />
- < /security>
- < /binding>
- < /basicHttpBinding>
- < /bindings>
- < client>
- < endpoint address="http://192.168.0.19:8008/mex"
binding="basicHttpBinding"- bindingConfiguration="BasicHttpBinding_IGetDataService"
contract="IGetDataService"- name="BasicHttpBinding_IGetDataService" />
- < /client>
- < /system.serviceModel>
以上就是我们为大家详细介绍的有关WCF Stream的操作方法。
分享名称:WCFStream实际应用功能体验
分享网址:http://www.shufengxianlan.com/qtweb/news41/253891.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联