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

源码来自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. P4949 最短距离(基环树+树链剖分)

    题目 P4949 最短距离 做法 先把非树边提出来 查询\((x,y)\)的最短距离就分类查询:树上\((x,y)\)距离,经过非树边距离 带边权查询链长,一个烂大街的套路:树链剖分,节点维护树边距离 ...

  2. LAMP脚本

    A goal is a dream with a deadline. Much effort, much prosperity. 环境:CentOS release 6.5  2.6.32-431.e ...

  3. Search insert position, 查找插入位置

    问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...

  4. scala学习手记4 - Java基本类型对应的scala类

    在Java中变量类型分为两大类:基本类型和引用类型.虽然在JDK1.5以后引入了自动装箱和自动拆箱机制,大大减少了我们在直接类型和引用类型之间的纠结,但仍有一些我们不得不考虑的问题.比如我在工作遇到的 ...

  5. js 技巧总结

    插件解析 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们为记录思想和分享知识提供更专业的工具. 您可以使 ...

  6. npm和git代理

    npm 删除代理设置:npm config delete proxynpm config delete https-proxynpm 设置代理:npm config set proxy http:// ...

  7. angularjs地址栏传参

    1:路由定义参数 2.controller 3. 4.目标得到参数值

  8. koa2使用&&中间件&&angular2的koa托管

    文章导航 1,koa2使用: 2,写中间件: 3,koa2路由配置angular2; 一.先上代码,一篇,看完koa2中大多基础方法: const Koa=require('koa'); const ...

  9. javascript简单介绍总结(一)

    DOM (Document Object Model)(文档对象模型)是用于访问 HTML 元素的正式 W3C 标准.在 HTML 中,JavaScript 语句向浏览器发出的命令.语句是用分号分隔: ...

  10. 29-THREE.JS 根据公式画形状

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...