WCF+上传+大文件处理

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

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

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

客户端

 1 public partial class WebForm1 : System.Web.UI.Page
 2     {
 3         protected void Page_Load(object sender, EventArgs e)
 4         {
 5 
 6         }
 7 
 8         protected void Button1_Click(object sender, EventArgs e)
 9         {
10             BigFileRead(@"E:\工具\开发工具\数据库\SqlServer\2008 r2\cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso");
11             Response.Write("文件传输成功!");
12         }
13 
14         private void BigFileRead(string strFilePath)
15         {
16             UploadService c = new UploadService();
17             //每次读取的字节数
18             int iBufferSize = 1024000;
19             byte[] buffer = new byte[iBufferSize];
20             FileStream fs = null;
21             try
22             {
23                 fs = new FileStream(strFilePath, FileMode.Open);
24                 //文件流的长度
25                 long lFileSize = fs.Length;
26                 //文件需要读取次数
27                 int iTotalCount = (int)Math.Ceiling((double)(lFileSize / iBufferSize));
28                 //当前读取次数
29                 int iTempCount = 0;
30 
31                 while (iTempCount < iTotalCount)
32                 {
33                     //每次从最后读到的位置读取下一个[iBufferSize]的字节数
34                     fs.Read(buffer, 0, iBufferSize);
35                     ////将字节转换成字符
36                     //string strRead = Encoding.Default.GetString(buffer);
37                     ////此处加入你的处理逻辑
38                     //Console.Write(strRead);
39                     c.UploadFile("aaa.iso",buffer);
40                     iTempCount++;
41                 }
42 
43             }
44             catch (Exception ex)
45             {
46                 //异常处理
47             }
48             finally
49             {
50                 if (fs != null)
51                 {
52                     fs.Dispose();
53                 }
54             }
55         }
56     }
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WcfService1.WebForm1" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <title></title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11     <div>
12     
13         <asp:FileUpload ID="FileUpload1" runat="server" />
14         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传" />
15     
16     </div>
17     </form>
18 </body>
19 </html>

 

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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3 
 4   <system.web>
 5     <compilation debug="true" targetFramework="4.0" />
 6       <httpRuntime   maxRequestLength="409600000" />
 7   </system.web>
 8     <system.serviceModel>
 9         <bindings>
10             <basicHttpBinding>
11                 <binding name="BasicHttpBinding_IUploadService" closeTimeout="00:01:00"
12                     openTimeout="00:01:00" receiveTimeout="01:10:00" sendTimeout="01:10:00"
13                     allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
14                     maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
15                     transferMode="Streamed"
16                     useDefaultWebProxy="true">
17                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
18                         maxBytesPerRead="4096" maxNameTableCharCount="16384" />
19                     <security mode="None">
20                         <transport clientCredentialType="None" proxyCredentialType="None"
21                             realm="" />
22                         <message clientCredentialType="UserName" algorithmSuite="Default" />
23                     </security>
24                 </binding>
25             </basicHttpBinding>
26         </bindings>
27         <client>
28             <endpoint address="http://localhost:40121/mex" binding="basicHttpBinding"
29                 bindingConfiguration="BasicHttpBinding_IUploadService" contract="IUploadService"
30                 name="BasicHttpBinding_IUploadService" />
31         </client>
32     </system.serviceModel>
33  <system.webServer>
34     <modules runAllManagedModulesForAllRequests="true"/>
35   </system.webServer>
36   
37 </configuration>

服务端代码

 1 /// <summary>
 2     /// 
 3     /// </summary>
 4     [ServiceContract]
 5     public interface IUploadService
 6     {
 7         /// <summary>
 8         /// 
 9         /// </summary>
10         /// <param name="fileName"></param>
11         /// <param name="fileBuffer"></param>
12         [OperationContract]
13         void UploadFile(string fileName, byte[] fileBuffer);
14     }
 1     /// <summary>
 2     /// 
 3     /// </summary>
 4     public class UploadService : IUploadService
 5     {
 6         /// <summary>
 7         /// 
 8         /// </summary>
 9         /// <param name="fileName">文件名</param>
10         /// <param name="fileBuffer">文件流字节</param>
11         public void UploadFile(string fileName,byte[] fileBuffer)
12         {
13             FileStream fs = new FileStream("D:\\" + fileName, FileMode.OpenOrCreate);
14             BinaryWriter writer = new BinaryWriter(fs);
15             try
16             {
17                 long offset = 0;//fs.Length;
18                 writer.Seek((int)offset, SeekOrigin.End);
19                 writer.Write(fileBuffer);
20             }
21             catch (Exception e)
22             {
23             }
24             finally
25             {
26                 writer.Close();
27                 writer.Dispose();
28                 fs.Close();
29             }
30         }
31     }

 

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

 

posted @ 2015-03-31 09:47  J.Y  阅读(828)  评论(0编辑  收藏  举报