Ask the Expert

Uploading files with Web services

How do we can upload a file using Web services from a client (Windows forms with C#.NET) as we can do file upload using enctype="multipart/form-data" in HTML?

    Requires Free Membership to View

A simple SOAP message sent over HTTP protocol usually has a content type of "text/xml". Although you could include a binary attachment as a parameter to a Web service method call, this carries overhead since the data is encoded as xs:base64 or xs:hexbinary (embedded or referenced), adding bloat to the message. Furthermore, it is inefficient for XML parsers to resolve these references, and unwrap the attachment while parsing the SOAP message. To address this several standards emerged for handling SOAP message attachments. SOAP with Attachments is the original W3C recommendation to define how SOAP messages are carried within a MIME "multipart/related" message, as a compound SOAP document. The first MIME part is the SOAP envelope, subsequent parts contain attachments. SOAP with Attachments addressed the problem of reducing payload overhead, but did not provide a formal way to associate attachments with the SOAP message.

DIME and WS-Attachments specifications evolved to improve on this. DIME is similar to MIME, but is an alternate binary message format that supports the compilation of a message with arbitrary types. Rather than a content type "multipart/related" it is packaged as "application/dime" so that those receiving the message can parse it accordingly. WS-Attachments describes how to encapsulate SOAP message in DIME.

To ultimately answer your question, the Web services toolkits support adding attachments to SOAP messages using an object model, so that you are abstracted from the details of populating HTTP headers and building the compound SOAP message. Thanks to the generation of Web service proxies, in a few lines of code you can create a DIME attachment from a binary stream, attach it to the outgoing attachments collection, and invoke the Web service method, as shown here for a .NET Web service.
DimeAttachment attachment =
                new DimeAttachment("image/jpeg",
                TypeFormatEnum.MediaType,
                data);
svc.RequestSoapContext.Attachments.Add(attachment);
svc.SignAttachment(text);      
In short, when you send attachments to a Web service, today's toolkits have standardized on DIME/WS-Attachments. This will likely evolve to accommodate encryption, digital signatures and other features not currently supported, and the names of the standards may even change, but for now this is the way to go.

As a teaser, I have included the links to some newly emerging discussions on SMTOM whose roots derive from PASWA. See below.

Specification links:
http://www.w3.org/TR/SOAP-attachments
http://www.ietf.org/internet-drafts/draft-nielsen-dime-soap-01.txt
http://www.ietf.org/internet-drafts/draft-nielsen-dime-02.txt
http://www.w3.org/TR/SOAP-attachments
http://www.w3.org/TR/soap12-mtom/
http://www.gotdotnet.com/team/jeffsch/paswa/paswa61.html

For more on DIME/WS-Attachments see the articles titled "Fun with WS-Attachments" and "Getting Started with WSE" at the following link: http://www.dotnetdashboard.net/DesktopDefault.aspx?tabindex=7&tabid=73

This was first published in March 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: