Use the DIME protocol to upload files in asp.net

Source: Internet
Author: User

In some Web Service application scenarios, such as the delivery of documents, the Word documents and other attachments are returned when the Web Service returns results. In this case, the DIME protocol can be used for file transmission. It is highly efficient to transmit SOAP messages without being serialized or deserialized. Of course, Web Services Enhancements (WSE) is used here. The latest version is 3.0. The version used in this article is 2.0sp2. It is interesting that the command space in each version of WSE has changed a lot. This is indeed a little distressing! We recommend that you install Visual Studio Tools when installing WSE, so that you do not need to manually modify the Web. config file of Web Service.
Sample download: http://www.cnblogs.com/Files/lcybest/DIMESample.rar
Web Service:
First, you must reference Microsoft. Web. Services2.dll, modify the Web. config file, and add the following Configuration:
<WebServices>
<SoapExtensionTypes>
<Add type = "Microsoft. web. services2.WebServicesExtension, Microsoft. web. services2, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "priority =" 1 "group =" 0 "/>
</SoapExtensionTypes>
</WebServices>
</System. web>
If you have installed the WSE Visual Studio tool, you can use the tool to implement the above work.
The following code demonstrates how to add a DIME attachment to ResponseSoapContext:
[WebMethod]
Public string GetDocument (string entity entid)
{
If (entid. Length = 0)
Return "entid can not be empty! ";
Attachment attach = new Attachment (Guid. NewGuid (). ToString (), @ "D: \ test.doc ");
Microsoft. Web. Services2.ResponseSoapContext. Current. Attachments. Add (attach );
Return "SendOK ";
}
We use a windows application to demonstrate the client that can receive Web Service attachments.
First, you must reference Microsoft. Web. Services2.dll to the project and add a reference to the Web Service. If the WSE tool is installed, a proxy class ending with "WSE" is automatically generated. This proxy class can be used directly in the code.
If no tool is installed, You need to manually modify the proxy class generated by Visual Studio. By default, the proxy class is from System. web. services. protocols. inherited by WebClientProtocol, which must be modified from Microsoft. web. services2.WebServicesClientProtocol to inherit.
In our client, we can use the following code to extract the files in Response and save them to the file system:
Program code
Private void button#click (object sender, System. EventArgs e)
{
TalkServer. DataInterface client = new DIMEClient. TalkServer. DataInterface ();
String strvalue = client. GetDocument ("test111 ");
If (client. ResponseSoapContext. Attachments. Count = 0)
{
MessageBox. Show ("No Attachments in the webservice response! ");
Return;
}
Microsoft. Web. Services2.Attachments. Attachment attach;
Attach = client. ResponseSoapContext. Attachments [0];
Byte [] buffer = new byte [attach. Stream. Length];
Client. ResponseSoapContext. Attachments [0]. Stream. Read (buffer, 0, buffer. Length );
System. IO. FileStream stream = new System. IO. FileStream (@ "C: \ test.doc", System. IO. FileMode. Create );
Stream. Write (buffer, 0, buffer. Length );
Stream. Flush ();
Stream. Close ();
If (strvalue = "SendOK ")
MessageBox. Show ("Receive succeed ");
Else
MessageBox. Show ("Receive fail ");
}

Related Article

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.