WCF利用Stream上传大文件

转自别人的文章,学习这个例子,基本上wcf也算入门了,接口用法、系统配置都有了

本文展示了在asp.net中利用wcf的stream方式传输大文件,解决了大文件上传问题。主要是存档方便以后遇到该问题是来查阅。贴出部分代码,如果有疑惑或需要完整代码的请留言 WebForm1.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
            FileData file = new FileData();             file.filename = FileUpload1.FileName;             file.data = FileUpload1.PostedFile.InputStream;             GetDataService c = new GetDataService();             c.UploadFile(file);             Response.Write("文件传输成功!");
}

IService1

namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IGetDataService
{
[OperationContract]
void UploadFile(FileData file);
}
[MessageContract]
public class FileData
{
[MessageHeader]
public string filename;
[MessageBodyMember]
public Stream data;
} }

Service1.svc

namespace WcfService1
{
public class GetDataService : IGetDataService
{
public void UploadFile(FileData file)
{ FileStream fs = new FileStream("D:\\我的项目\\WcfService1\\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); fs.Close();
file.data.Close(); }
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{ fs.Close();
file.data.Close(); } }
}

web.config

<configuration>

  <system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="40960" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IGetDataService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="01:10:00" sendTimeout="01: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://localhost:52884/mex" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IGetDataService" contract="IGetDataService"
name="BasicHttpBinding_IGetDataService" />
</client>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer> </configuration>

看到有好几位同学想要源码的,我重新做了个测试工程,经测试只要网络支持,是可以上传几十M以上的大文件的

附上测试工程项目源码:http://files.cnblogs.com/easywebfactory/WcfService1.rar

WCF利用Stream上传大文件的更多相关文章

  1. Asp.net MVC利用WebUploader上传大文件出现404解决办法。

    刚开始我上传小文件都是比较顺利的,但是上传了一个大文件大约有200M的压缩包就不行了.在chrome里面监视发现网络状态是404,我分析可能不是WebUploader的限制,应该是WebConfig限 ...

  2. PHP上传大文件 分割文件上传

    最近遇到这么个情况,需要将一些大的文件上传到服务器,我现在拥有的权限是只能在一个网页版的文件管理系统来进行操作,可以解压,可以压缩,当然也可以用它来在线编辑.php文件. 文件有40M左右,但是服务器 ...

  3. WEB上传大文件解决方案

    众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路.下面贴出简易 ...

  4. C# 上传大文件

    上传大文件首先要修改web.config文件,否则上传报错.在web.config添加如下配置maxRequestLength表示能上传的最大文件值,单位是KB,requestLengthDiskTh ...

  5. WEB上传大文件

    众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路.下面贴出简易 ...

  6. [Asp.net]Uploadify上传大文件,Http error 404 解决方案

    引言 之前使用Uploadify做了一个上传图片并预览的功能,今天在项目中,要使用该插件上传大文件.之前弄过上传图片的demo,就使用该demo进行测试.可以查看我的这篇文章:[Asp.net]Upl ...

  7. php 上传大文件配置upload_max_filesize和post_max_size选项

    php 上传大文件配置upload_max_filesize和post_max_size选项 (2014-04-29 14:42:11) 转载▼ 标签: php.ini upload _files[f ...

  8. ASP.NET上传大文件的问题

    原文:http://www.cnblogs.com/wolf-sun/p/3657241.html?utm_source=tuicool&utm_medium=referral 引言 之前使用 ...

  9. php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项

    php 上传大文件主要涉及配置 upload_max_filesize 和post_max_size两个选项   今天在做上传的时候出现一个非常怪的问题,有时候表单提交可以获取到值,有时候就获取不到了 ...

随机推荐

  1. VC++ 利用PDB和dump文件定位问题并进行调试

    转载:https://blog.csdn.net/zfs_kuai/article/details/43646665 转载:https://blog.csdn.net/i_chaoren/articl ...

  2. 尚硅谷面试第一季-09SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢

    目录结构: 关键代码: web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> &l ...

  3. CocoaPods出错

    1 Error: pod search Masonry /usr/local/lib/ruby/gems/2.3.0/gems/cocoapods-1.4.0.beta.2/lib/cocoapods ...

  4. How to Rerun Failed Tests in JUnit?

    该帖转自其他出处 Sometimes due to some temporarily problems such as connection problems, server problems, br ...

  5. (一)flutter第一天

    import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...

  6. Docker 入门指南——资源工具篇

    好工具 dive wagoodman/dive A tool for exploring each layer in a docker image 参考 用 Dive 看 Docker Image 裡 ...

  7. 轻重搭配|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    样例输入: 6 1 9 7 3 5 5 样例输出: 4 思路:贪心,选错贪心思路,只能过一小部分数据,正确贪心思路:从前一半遍历,在后一半中找到比当前元素的两倍大的数(因为这里指针不会后移,所以可以采 ...

  8. 初识Java框架

    Spring boot>spring>spring mvc SSH:struts2+spring+hibernate SSM(SSH的改进): (过去)spring+struts2+MyB ...

  9. Oracle联合多个子查询(inner join)

    select aaa.*,bbb.xh from (select xn,xq,kcdm,kcmc,xf,xkkh,kcxz from jxrwbview where xn='2017-2018' gr ...

  10. python 之 知识点(1)

    在python是使用bif=built in functions 即内置函数 dir(__builtins__) 可以查看所有的内置函数.注:pycharm中无法使用,不知道原因 help(input ...