Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upload Files by WCF

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to upload files on WCF. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

I want to upload a video on Youku. Have you ever thought about how the platform is realized? Here I would like to analyze for you about uploading files using WCF. Before WCF appeared, I always used WebService to upload files. I don't know why others did this, because our file server and website background and website foreground are not on the same machine. The operator thought that uploading files with FTP was too troublesome, so I made a special WebService for uploading files and deployed this WebService on the file server. Then call the WebService in the background of the website, convert the file uploaded to the background page of the site into bytes and pass it to WebService, and then WebService saves the byte stream to a site that only allows static pages (static websites can prevent some script Trojans).

WebService to upload files is not efficient, and can not transfer a large amount of data files, of course, you can use MTOM in Wse to transfer large files, with WCF is much better, through the use of WCF to transfer Stream objects to transfer big data files, but there are some restrictions: only BasicHttpBinding, NetTcpBinding and NetNamedPipeBinding support transport stream data. The stream data type must be serializable Stream or MemoryStream. No other data can be contained in the message body (MessageBody) when delivered. The restrictions of TransferMode and MaxReceivedMessageSize, etc.

The following specific implementation: create a new WCFService. The code of the API file is as follows:

[ServiceContract] publicinterfaceIUpLoadService {[OperationContract (Action= "UploadFile", IsOneWay=true)] voidUploadFile (FileUploadMessagerequest);} [MessageContract] publicclassFileUploadMessage {[MessageHeader (MustUnderstand=true)] publicstringSavePath; [MessageHeader (MustUnderstand=true)] publicstringFileName; [MessageBodyMember (Order=1)] publicStreamFileData;}

The purpose of defining the FileUploadMessage class is due to the third restriction, otherwise the file name and storage path cannot be passed to WCF. According to the second restriction, there is only one API method for transferring file data using System.IO.Stream, that is, uploading files by WCF. Note that the method parameter is the code of the class file implemented by FileUploadMessage API:

PublicclassUpLoadService:IUpLoadService {publicvoidUploadFile (FileUploadMessagerequest) {stringuploadFolder=@ "C:\ kkk\"; stringsavaPath=request.SavePath; stringdateString=DateTime.Now.ToShortDateString () + @ "\"; stringfileName=request.FileName; StreamsourceStream=request.FileData; FileStreamtargetStream=null; if (! sourceStream.CanRead) {thrownewException ("data flow is not readable!");} if (savaPath==null) savaPath=@ "Photo\"; if (! savaPath.EndsWith ("\\") savaPath+= "\"; uploadFolderuploadFolder=uploadFolder+savaPath+dateString If (! Directory.Exists (uploadFolder)) {Directory.CreateDirectory (uploadFolder);} stringfilePath=Path.Combine (uploadFolder,fileName); using (targetStream=newFileStream (filePath,FileMode.Create,FileAccess.Write,FileShare.None)) {/ / readfromtheinputstreamin4Kchunks / / andsavetooutputstream constintbufferLen=4096; byte [] buffer= newbyte [bufferLen]; intcount=0; while ((count=sourceStream.Read (buffer,0,bufferLen)) > 0) {targetStream.Write (buffer,0,count);} targetStream.Close (); sourceStream.Close ();}

The function of the implementation is to divide the directory by date under the specified directory, and then save the file with the passed file name. The main part of this article is the following Web.Config configuration:

After reading the above, do you have any further understanding of how to upload files on WCF? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report