av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

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

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

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

WebService來(lái)上傳文件存在的問(wèn)題是效率不高,而且不能傳輸大數(shù)據(jù)量的文件,當(dāng)然你可以用Wse中的MTOM來(lái)傳輸大文件,有了WCF就好多了,通過(guò)使用WCF傳遞Stream對(duì)象來(lái)傳遞大數(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;
  16. }

定義FileUploadMessage類的目的是因?yàn)榈谌齻€(gè)限制,要不然文件名和存放路徑就沒(méi)辦法傳遞給WCF了,根據(jù)第二個(gè)限制,文件數(shù)據(jù)是用System.IO.Stream來(lái)傳遞的

接口方法只有一個(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. 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. }

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

這篇文章最主要的地方就是下面的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)目測(cè)試一下。


分享標(biāo)題:WCF上傳文件實(shí)際應(yīng)用技巧講解
URL地址:http://uogjgqi.cn/article/cdcpeoj.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流