Using stream to upload large files

Source: Internet
Author: User

Use message contract to upload large files in WCF.

First, you must prepare a data carrier class and a WCF Service.

[Servicecontract]
Public interface igetdataservice
{
[Operationcontract]
Void uploadfile (filedata file );
}
[Messagecontract]
Public class filedata
{
[Messageheader]
Public String filepath {set; get ;}

[Messagebodymember (Order = 1)]
Public stream filedata {set; get ;}
}

The specific implementation method of the server is followed.

[operationcontract]
Public void uploadmodelfile (fileuploadmessage physicalfile)
{< br> try
{< br> // Upload File
stream sourcestream = physicalfile. filedata;
If (! Sourcestream. Canread)
throw new ioexception ("the input stream can not be read! ");

String uploadfolder = physicalfile. filepath. substring (0, physicalfile. filepath. lastindexof ("\\"));
If (! Directory. exists (uploadfolder ))
Directory. createdirectory (uploadfolder );

Using (filestream FS = new filestream (physicalfile. filepath, filemode. Create, fileaccess. Write, fileshare. None ))
{
// Read from the input stream in 4 K chunks
// And save to output stream
Const int bufferlen = 4096;
Byte [] buffer = new byte [bufferlen];
Int COUNT = 0;
While (COUNT = sourcestream. Read (buffer, 0, bufferlen)> 0)
{
FS. Write (buffer, 0, count );
}
FS. Close ();
Sourcestream. Close ();
}
}
Catch (exception ex)
{
Logmanager. getinstance (). writeerrorlog (string. Format ("Upload model file fail! Message: {0} ", Ex. Message ));
Throw ex;
}
}

Finally, you can use the client class of WCF to call the service.

 

CodePay attention to the following two points during implementation:

1. the type of the return value (or input parameter type) of the operationcontract method that uses the fileuploadmessage class as the input parameter must be the type used in the fileuploadmessage class. For example, attributes in the fileuploadmessage class include string and stream, the parameter type of the uploadmodelfile method can only be string or stream. Otherwise, an error is returned. However, the most secure response is to return the void, so that no error is returned.

2. It is also a parameter type Problem of the uploadmodelfile method. The parameter type marked as messagecontractattribute cannot be mixed with the parameter marked as datacontractattribute. Otherwise, an error is reported.

 

Pay attention to the following points during the WCF configuration process:

1. In the client configuration file that uses the WCF Service, remember to set the value of maxbuffersize and the value of maxcompute edmessagesize. Otherwise, an error is reported when the file is large. The unit here is byte.

 <  Bindings  >
< Basichttpbinding >
< Binding Name = " Basichttpbinding_wcfservice " Maxbuffersize = " 2147483647 "
Maxcompute edmessagesize = " 2147483647 " >
< Security Mode = " None " />
</ Binding >
</ Basichttpbinding >
</ Bindings >
2. Compile Service Add bindings nodes to the server
 <  Bindings  >
< Basichttpbinding >
< Binding Name = " Largedatatransferservicesbinding " Maxcompute edmessagesize = " 2147483647 "
Messageencoding = " Text " Transfermode = " Streamed " Sendtimeout = " 00:10:00 " />
</ Basichttpbinding >
</ Bindings >

this node does not seem to be configured by default. You must manually add it on the system. in the servicemodel node, add the bindingconfiguration = "largedatatransferservicesbinding" attribute to the basichttpbinding node of the download service endpoint.
name =" filetransferservice ">



for detailed configuration, see http://www.cnblogs.com/cuihongyu3503319/archive/2010/09/01/1814394.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.