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

源码来自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

最新文章

  1. linux: 常用copy 命令
  2. JQuery slidebox实现图片轮播
  3. JiaThis分享插件的使用
  4. select2初始化默认值
  5. codeforces 422A A. Borya and Hanabi(暴力)
  6. python datetime
  7. ARM Linux从Bootloader、kernel到filesystem启动流程
  8. 非堵塞socket实现android手机与PC的文件传输
  9. 浙大pat1020题解
  10. Logger日志管理工具类
  11. Redis主从和HA配置
  12. SharePoint 2007 列表页定制--4个默认页定制
  13. 程序猿想聊天 - 創問 4C 團隊教練心得(一)
  14. git教程——安装配置
  15. 上传前端webuploader
  16. 暗之的锁链 [COGS2434] [树上差分]
  17. socket之 select模型
  18. Android笔记:Button
  19. Oracle问题诊断过程常用SQL
  20. 0605-Zuul构建API Gateway-使用Sidecar支持异构平台的微服务

热门文章

  1. Winform与WPF异步修改控件属性
  2. HDU 1159 Common Subsequence 动态规划
  3. handle 机制的原理是什么
  4. windows开dump
  5. Linux 更改默认安装路径
  6. Pandas索引和选择数据
  7. yii2定时任务不执行,报错PHP date调用警告:It is not safe to rely on the system&#39;s timezone settings
  8. javascript 对象简单介绍(二)
  9. javascript简单介绍总结(一)
  10. JavaScript中的两个“0” -0和+0