大文件处理的方式拆分读取,此文只为记录文件处理方式,供日后查阅。

源码来自http://blog.csdn.net/lywust/article/details/7009248

经过改编将源码改编为文件上传的功能如下(处理几个GB的文件应该是没有问题的)

客户端

 public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
BigFileRead(@"E:\工具\开发工具\数据库\SqlServer\2008 r2\cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso");
Response.Write("文件传输成功!");
} private void BigFileRead(string strFilePath)
{
UploadService c = new UploadService();
//每次读取的字节数
int iBufferSize = ;
byte[] buffer = new byte[iBufferSize];
FileStream fs = null;
try
{
fs = new FileStream(strFilePath, FileMode.Open);
//文件流的长度
long lFileSize = fs.Length;
//文件需要读取次数
int iTotalCount = (int)Math.Ceiling((double)(lFileSize / iBufferSize));
//当前读取次数
int iTempCount = ; while (iTempCount < iTotalCount)
{
//每次从最后读到的位置读取下一个[iBufferSize]的字节数
fs.Read(buffer, , iBufferSize);
////将字节转换成字符
//string strRead = Encoding.Default.GetString(buffer);
////此处加入你的处理逻辑
//Console.Write(strRead);
c.UploadFile("aaa.iso",buffer);
iTempCount++;
} }
catch (Exception ex)
{
//异常处理
}
finally
{
if (fs != null)
{
fs.Dispose();
}
}
}
}
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WcfService1.WebForm1" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传" /> </div>
</form>
</body>
</html>

配置文件(因在一个工程下所以包含服务端和客户端配置文件)

 <?xml version="1.0" encoding="utf-8"?>
<configuration> <system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IUploadService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="01:10:00" sendTimeout="01:10:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="" maxBufferPoolSize="" maxReceivedMessageSize=""
transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength=""
maxBytesPerRead="" maxNameTableCharCount="" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:40121/mex" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IUploadService" contract="IUploadService"
name="BasicHttpBinding_IUploadService" />
</client>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer> </configuration>

服务端代码

 /// <summary>
///
/// </summary>
[ServiceContract]
public interface IUploadService
{
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="fileBuffer"></param>
[OperationContract]
void UploadFile(string fileName, byte[] fileBuffer);
}
     /// <summary>
///
/// </summary>
public class UploadService : IUploadService
{
/// <summary>
///
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="fileBuffer">文件流字节</param>
public void UploadFile(string fileName,byte[] fileBuffer)
{
FileStream fs = new FileStream("D:\\" + fileName, FileMode.OpenOrCreate);
BinaryWriter writer = new BinaryWriter(fs);
try
{
long offset = 0;//fs.Length;
writer.Seek((int)offset, SeekOrigin.End);
writer.Write(fileBuffer);
}
catch (Exception e)
{
}
finally
{
writer.Close();
writer.Dispose();
fs.Close();
}
}
}

代码下载 大文件处理[上传].rar

WCF+上传+大文件处理的更多相关文章

  1. WCF上传大文件处理方法

    <system.serviceModel> <bindings> <basicHttpBind> <Binding name=" maxReceiv ...

  2. WCF利用Stream上传大文件

    WCF利用Stream上传大文件 转自别人的文章,学习这个例子,基本上wcf也算入门了,接口用法.系统配置都有了 本文展示了在asp.net中利用wcf的stream方式传输大文件,解决了大文件上传问 ...

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

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

  4. 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 ...

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

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

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

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

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

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

  8. SWFUpload上传大文件(暂时用用,真正用的时候还是要改的)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. PHP上传大文件和处理大数据

    1. 上传大文件 /* 以1.5M/秒的速度写入文件,防止一次过写入文件过大导致服务器出错(chy/20150327) */ $is_large_file = false; if( strlen($x ...

随机推荐

  1. 【META http-equiv="Content-Type" Content="text/html; Charset=*】意义详解

    [META http-equiv="Content-Type" Content="text/html; Charset=*]意义详解 META,网页Html语言里Head ...

  2. h5新特性--- 多媒体元素

    在H5中只有一行代码即可实现在页面中插入视频 <video src="插入的视频的名字" controls></video> 可以指明视频的宽度和高度 &l ...

  3. RN app打包

    最近使用React Native做起了移动应用,之前做过一点react,有一点react基础,后来听说RN还不错,就做起了RN项目.为了让辛辛苦苦开发的项目想在手机端运行,就涉及到发布打包. 防止自己 ...

  4. Linux下多线程下载工具MWget和Axel使用介绍

    linux运维在操作linux过程中,用得最多的linux下载工具想必一定是wget,没有看到哪一台服务器没装过wget的,或许有人使用ftp下载,也有人使用多线程的axel以及ProZilla,毫无 ...

  5. JavaEE之注解

    1注解:Annotation注解,是一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次,给计算机,JVM提供解读信息的. 2注解的作用:编译检查:代码分析,编 ...

  6. 关于Block汇总

    //使用总结: //1.当block里面会有b类相关的参数要回调回去的时候,属性用copy修饰,将其拷贝到堆里面,这样即便栈释放掉了,b类的指针也在堆中存在,能够成功的回调回去. //Block默认存 ...

  7. VMware Workstation 12 增加磁盘容量 Windows Server 2012 系统 扩展

    1.安装虚拟机后,检查C盘容量大小,发现C盘现在的空间是59.9GB,如下图: 2.使用window+R键,出现运行窗口,输入‘cmd’——>‘cd C:\Program Files (x86) ...

  8. SSO 证书配置

    ssodev.crt为开发环境证书ssotest.crt为测试环境证书 将证书拷贝到目录:{JDK}\jre\lib\security 其中 {JDK} 是实际安装JDK的位置.然后cmd进入命令窗口 ...

  9. merge two sorted lists, 合并两个有序序列

    /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Lis ...

  10. 报错HTTP Status 500 - Unable to instantiate Action

    报错如下: HTTP Status 500 - Unable to instantiate Action, visitAction, defined for 'visit_toAddPage' in ...