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

源码来自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. Python3.x:第三方库简介

    Python3.x:第三方库简介 环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex ...

  2. 大牛是怎么思考设计MySQL优化方案的?

    在进行MySQL的优化之前,必须要了解的就是MySQL的查询过程,很多查询优化工作实际上就是遵循一些原则,让MySQL的优化器能够按照预想的合理方式运行而已. 图-MySQL查询过程 一.优化的哲学 ...

  3. 如何用纯 CSS 创作一个小球上台阶的动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/PBGJwL 可交互视频 ...

  4. SQuirrel-GUI工具安装手册-基于phoenix驱动

    背景描述: SQuirrel sql client 官方地址:http://www.squirrelsql.org/index.php?page=screenshots 一个图形界面的管理工具 安装手 ...

  5. 20145235李涛《网络对抗》Exp7 网络欺诈技术防范

    基础问题回答 通常在什么场景下容易受到DNS spoof攻击? 使用未知的公共wifi或者在不安全的局域网下容易受到DNS spoof攻击. 在日常生活工作中如何防范以上两攻击方法? 首先要提高防范意 ...

  6. win10安装z3求解器

    因为课程要求,我不得不接触求解器,之前有在ubuntu上装过一个叫stp的求解器,没怎么用: 今天在我的电脑(win10)上上装了一款更方便的求解器---z3,下面先详细介绍一下怎么安装和配置: 1. ...

  7. React Native Could not expand ZIP

    Execution failed for task ':app:prepareSrolkReactNativeFilePickerUnspecifiedLibrary'. Could not expa ...

  8. sublime使用sublimelint-luacheck屏蔽指定警告

    在成功安装SublimeLinter-lua与luacheck以后,如果没有语法error,则会进行警告提示. 如下图 waring: line contains trailing whitespac ...

  9. 智能穿戴设备移动APP端与外设数据传输协议功能模块CMD&ACK表

    Notification Module Function CMD ACK Notification History Count [0x0301] [0x0000] [0x01] [0x0301] [0 ...

  10. java用servlet、cookie实现一个阅读记录

    效果如图 代码1 package com.xiaostudy.servlet; import java.io.IOException; import java.io.PrintWriter; impo ...