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

WCF上傳文件實(shí)際應(yīng)用技巧講解

開發(fā) 開發(fā)工具
WCF上傳文件是在實(shí)際編程中經(jīng)常遇到的一個(gè)問題。對(duì)于初學(xué)者來說,必須充分掌握這一基本常識(shí),夯實(shí)我們所掌握的知識(shí)。

WCF開發(fā)框架可以幫助我們滿足許多功能需求。在這里我們?yōu)榇蠹以敿?xì)介紹有關(guān)WCF上傳文件的相關(guān)應(yīng)用技巧。希望對(duì)大家有所幫助。#t#

在WCF沒出現(xiàn)之前,我一直使用用WebService來上傳文件,我不知道別人為什么要這么做,因?yàn)槲覀兊奈募?wù)器和網(wǎng)站后臺(tái)和網(wǎng)站前臺(tái)都不在同一個(gè)機(jī)器,操作人員覺得用FTP傳文件太麻煩,我就做一個(gè)專門用來上傳文件的WebService,把這個(gè)WebService部署在文件服務(wù)器上,然后在網(wǎng)站后臺(tái)調(diào)用這個(gè)WebService,把網(wǎng)站后臺(tái)頁面上傳上來的文件轉(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對(duì)象來傳遞大數(shù)據(jù)文件,但WCF上傳文件有一些限制:

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è),就是上傳文件,注意方法參數(shù)是FileUploadMessage

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

  1. public class UpLoadService : 
    IUpLoadService  
  2. {  
  3. public void UploadFile(File
    UploadMessage 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.  
  23. string filePath = Path.Combine(upload
    Folder, fileName);  
  24. using (targetStream = new FileStream
    (filePath, FileMode.Create, FileAccess.
    Write, FileShare.None))  
  25. {  
  26. //read from the input stream in 4K chunks  
  27. //and save to output stream  
  28. const int bufferLen = 4096;  
  29. byte[] buffer = new byte[bufferLen];  
  30. int count = 0;  
  31. while ((count = sourceStream.Read
    (buffer, 0, bufferLen)) 
    > 0)  
  32. {  
  33. targetStream.Write(buffer, 0, count);  
  34. }  
  35. targetStream.Close();  
  36. sourceStream.Close();  
  37. }  
  38. }  

 

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

這篇文章最主要的地方就是下面的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上傳文件就完成了,新建一個(gè)Console項(xiàng)目或者Web項(xiàng)目測試一下。

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

2010-02-23 14:17:20

WCF配置文件

2010-03-01 13:06:49

WCF繼承

2010-02-22 16:19:25

WCF自托管

2010-02-22 13:28:05

WCF異步調(diào)用

2010-02-26 13:40:28

WCF消息頭

2010-03-01 17:52:03

WCF選擇綁定

2010-02-23 09:44:12

WCF dataCon

2010-03-02 10:54:42

WCF回調(diào)操作

2010-02-23 10:57:34

WCF Streami

2009-12-22 19:14:36

WCF效率

2010-02-22 17:58:06

WCF異步上傳

2010-02-22 11:25:50

WCF DateSet

2010-02-24 13:48:44

MSMQ使用WCF

2010-02-24 15:20:23

WCF Message

2010-02-26 10:56:06

WCF Stream

2010-03-02 17:35:20

WCF服務(wù)加載

2010-02-22 16:26:47

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

2010-02-24 09:38:58

WCF應(yīng)用編碼

2010-02-23 10:25:29

2010-02-24 14:05:08

WCF openati
點(diǎn)贊
收藏

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