日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WCF上傳文件實際應(yīng)用技巧講解

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

目前創(chuàng)新互聯(lián)已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、廬陽網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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

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

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

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

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

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

下面具體實現(xiàn):新建一個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;
  16. }

定義FileUploadMessage類的目的是因為第三個限制,要不然文件名和存放路徑就沒辦法傳遞給WCF了,根據(jù)第二個限制,文件數(shù)據(jù)是用System.IO.Stream來傳遞的

接口方法只有一個,就是上傳文件,注意方法參數(shù)是FileUploadMessage

接口實現(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. string filePath = Path.Combine(upload
    Folder, 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. }
  38. }

實現(xià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>

配置要遵循上面的第一條和第四條限制,因為默認.net只能傳4M的文件,所以要在< System.Web>配置節(jié)下面加上< httpRuntimemaxRequestLength="2097151" />。這樣WCF上傳文件就完成了,新建一個Console項目或者Web項目測試一下。


新聞標題:WCF上傳文件實際應(yīng)用技巧講解
分享鏈接:http://www.5511xx.com/article/cdcpeoj.html