自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF上傳文件解決方案剖析

開發(fā) 開發(fā)工具
我們今天會在這篇文章中為大家詳細(xì)介紹一下WCF上傳文件的相關(guān)實(shí)現(xiàn)方法,以方便大家在實(shí)際應(yīng)用中獲得一些幫助。

WCF是一個(gè).NET Framework 3.5的重要組成部分,它的書籍應(yīng)用給開發(fā)人員帶來了非常大的幫助。我們今天就可以先從WCF上傳文件的相關(guān)步驟,來分析一下這一工具的簡單實(shí)現(xiàn)方法,幫助我們理解應(yīng)用。#t#

在WCF沒出現(xiàn)之前,我一直使用用WebService來上傳文件,我不知道別人為什么要這么做,因?yàn)槲覀兊奈募?wù)器和網(wǎng)站后臺和網(wǎng)站前臺都不在同一個(gè)機(jī)器,操作人員覺得用FTP傳文件太麻煩,我就做一個(gè)專門用來上傳文件的WebService,把這個(gè)WebService部署在文件服務(wù)器上,然后在網(wǎng)站后臺調(diào)用這個(gè)WebService,把網(wǎng)站后臺頁面上傳上來的文件轉(zhuǎn)化為字節(jié)流傳給WebService,然后WebService把這個(gè)字節(jié)流保存文件到一個(gè)只允許靜態(tài)頁面的網(wǎng)站(靜態(tài)網(wǎng)站可以防止一些腳本木馬)。

WebService來上傳文件存在的問題是效率不高,而且不能傳輸大數(shù)據(jù)量的文件,當(dāng)然你可以用Wse中的MTOM來傳輸大文件,有了WCF就好多了,通過使用WCF傳遞Stream對象來傳遞大數(shù)據(jù)文件,但有一些限制:

1、只有 BasicHttpBinding、NetTcpBinding 和 NetNamedPipeBinding 支持傳送流數(shù)據(jù)。

2、 流數(shù)據(jù)類型必須是可序列化的 Stream 或 MemoryStream。

3、 傳遞時(shí)消息體(Message Body)中不能包含其他數(shù)據(jù)。

4、TransferMode的限制和MaxReceivedMessageSize的限制等。

下面具體實(shí)現(xiàn):新建一個(gè)WCFService,接口文件的代碼如下:

  1. [ServiceContract]  
  2. public interface IUpLoadService  
  3. {  
  4. [OperationContract(Action = "UploadFile"IsOneWay = true)]  
  5. void UploadFile(FileUploadMessage request);  
  6. }  
  7. [MessageContract]  
  8. public class FileUploadMessage  
  9. {  
  10. [MessageHeader(MustUnderstand = true)]  
  11. public string SavePath;  
  12. [MessageHeader(MustUnderstand = true)]  
  13. public string FileName;  
  14. [MessageBodyMember(Order = 1)]  
  15. public Stream FileData;  

 

定義FileUploadMessage類的目的是因?yàn)榈谌齻€(gè)限制,要不然文件名和存放路徑就沒辦法傳遞給WCF了,根據(jù)第二個(gè)限制,文件數(shù)據(jù)是用System.IO.Stream來傳遞的接口方法只有一個(gè),就是WCF上傳文件,注意方法參數(shù)是FileUploadMessage

接口實(shí)現(xiàn)類文件的代碼如下:

  1. public class UpLoadService : IUpLoadService  
  2. {  
  3. public void UploadFile(FileUploadMessage request)  
  4. {  
  5. string uploadFolder = @"C:\kkk\";  
  6. string savaPath = request.SavePath;  
  7. string dateString = DateTime.Now.ToShortDateString() + @"\";  
  8. string fileName = request.FileName;  
  9. Stream sourceStream = request.FileData;  
  10. FileStream targetStream = null;  
  11. if (!sourceStream.CanRead)  
  12. {  
  13. throw new Exception("數(shù)據(jù)流不可讀!");  
  14. }  
  15. if (savaPath == null) savaPath = @"Photo\";  
  16. if (!savaPath.EndsWith("\\")) savaPath += "\\";  
  17. uploadFolderuploadFolder = uploadFolder + savaPath + dateString;  
  18. if (!Directory.Exists(uploadFolder))  
  19. {  
  20. Directory.CreateDirectory(uploadFolder);  
  21. }  
  22. string filePath = Path.Combine(uploadFolder, fileName);  
  23. using (targetStream = new FileStream(filePath, FileMode.Create, 
    FileAccess.Write, FileShare.None))  
  24. {  
  25. //read from the input stream in 4K chunks  
  26. //and save to output stream  
  27. const int bufferLen = 4096;  
  28. byte[] buffer = new byte[bufferLen];  
  29. int count = 0;  
  30. while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)  
  31. {  
  32. targetStream.Write(buffer, 0, count);  
  33. }  
  34. targetStream.Close();  
  35. sourceStream.Close();  
  36. }  
  37. }  

 

實(shí)現(xiàn)的功能是到指定目錄下按照日期進(jìn)行目錄劃分,然后以傳過來的文件名保存文件。

這篇WCF上傳文件的文章最主要的地方就是下面的Web.Config配置:

 

 

 

  1. < system.serviceModel> 
  2. < bindings> 
  3. < basicHttpBinding> 
  4. < binding name="FileTransferServicesBinding" 
    maxReceivedMessageSize="9223372036854775807" 
  5. messageEncoding="Mtom" transferMode="Streamed" sendTimeout="00:10:00" /> 
  6. < /basicHttpBinding> 
  7. < /bindings> 
  8. < services> 
  9. < service behaviorConfiguration="UploadWcfService.UpLoadServiceBehavior" 
  10. name="UploadWcfService.UpLoadService"> 
  11. < endpoint address="" binding="basicHttpBinding" bindingConfiguration=
    "FileTransferServicesBinding" contract="UploadWcfService.IUpLoadService"> 
  12. < /endpoint> 
  13. < endpoint address="mex" binding="mexHttpBinding" 
    contract="IMetadataExchange" /> 
  14. < /service> 
  15. < /services> 
  16. < behaviors> 
  17. < serviceBehaviors> 
  18. < behavior name="UploadWcfService.UpLoadServiceBehavior"> 
  19. < serviceMetadata httpGetEnabled="true" /> 
  20. < serviceDebug includeExceptionDetailInFaults="false" /> 
  21. < /behavior> 
  22. < /serviceBehaviors> 
  23. < /behaviors> 
  24. < /system.serviceModel> 

 

 

配置要遵循上面的***條和第四條限制,因?yàn)槟J(rèn).net只能傳4M的文件,所以要在< System.Web>配置節(jié)下面加上< httpRuntimemaxRequestLength="2097151" />

以上就是對WCF上傳文件的相關(guān)介紹。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-02-23 14:56:18

WCF Bug

2009-12-07 15:50:27

WCF文件

2010-02-24 09:28:37

WCF安全配置

2010-02-24 14:05:08

WCF openati

2010-02-26 15:46:48

Silverlight

2009-11-06 15:25:25

WCF異常

2010-02-25 14:53:44

WCF調(diào)用服務(wù)異常

2009-12-08 15:19:58

WCF大數(shù)據(jù)量

2009-12-30 14:31:57

PPPoA體系

2009-01-19 17:31:14

2009-11-16 09:45:51

PHP上傳文件大小

2009-11-16 13:46:28

PHP上傳文件大小限制

2010-02-23 17:49:56

WCF傳輸大數(shù)據(jù)

2010-02-24 10:55:01

WCF跨域訪問

2009-11-09 11:31:47

WCF消息隊(duì)列

2010-08-03 14:58:38

APC

2010-09-27 16:23:28

APC

2009-01-16 18:11:01

2017-07-04 15:10:20

移動端圖片旋轉(zhuǎn)壓縮

2016-04-18 15:50:09

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號