This topic has not yet been rated - Rate this topic

ContentDisposition Class

Represents a MIME protocol Content-Disposition header.

System.Object
  System.Net.Mime.ContentDisposition

Namespace:  System.Net.Mime
Assembly:  System (in System.dll)
public class ContentDisposition

The ContentDisposition type exposes the following members.

  Name Description
Public method ContentDisposition() Initializes a new instance of the ContentDisposition class with a DispositionType of Attachment.
Public method ContentDisposition(String) Initializes a new instance of the ContentDisposition class with the specified disposition information.
Top
  Name Description
Public property CreationDate Gets or sets the creation date for a file attachment.
Public property DispositionType Gets or sets the disposition type for an e-mail attachment.
Public property FileName Gets or sets the suggested file name for an e-mail attachment.
Public property Inline Gets or sets a Boolean value that determines the disposition type (Inline or Attachment) for an e-mail attachment.
Public property ModificationDate Gets or sets the modification date for a file attachment.
Public property Parameters Gets the parameters included in the Content-Disposition header represented by this instance.
Public property ReadDate Gets or sets the read date for a file attachment.
Public property Size Gets or sets the size of a file attachment.
Top
  Name Description
Public method Equals Determines whether the content-disposition header of the specified ContentDisposition object is equal to the content-disposition header of this object. (Overrides Object.Equals(Object).)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Determines the hash code of the specified ContentDisposition object (Overrides Object.GetHashCode().)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a String representation of this instance. (Overrides Object.ToString().)
Top

The information in the ContentDisposition class accompanies an e-mail message that contains attachments when the e-mail message is sent to its destination. The information in ContentDisposition can be used by software that displays e-mail to present the e-mail attachments in the manner intended by the sender.

E-mail messages are created using instances of the MailMessage class. Instances of the Attachment class are used to add attachments to e-mail messages. To modify the ContentDisposition for an attachment, get the instance from the Attachment.ContentDisposition property.

Content to be displayed as part of the message body has the disposition type of Inline. Content that is not displayed but is attached in a separate file has the disposition type of Attachment. Use the Inline property to control the disposition type for the attachment associated with an instance of ContentDisposition.

For file attachments, you can use the properties of the ContentDisposition to set the file size, as well as the date the file was created, last read, and last modified. For all attachments, you can set a recommended file name in the event that the attachment is stored on the receiving computer.

The ToString method returns the Content-Disposition header. The Content-Disposition header is described in RFC 2183 available at http://www.ietf.org.

The following code example creates an e-mail message with an attachment to be displayed inline.


		public static void CreateMessageInlineAttachment(string server, string
		textMessage)
		{
			// Create a message and set up the recipients.
			MailMessage message = new MailMessage(
			   "jane@contoso.com",
			   "ben@contoso.com",
			   "An inline text message for you.",
			   "Message: ");

			// Attach the message string to this e-mail message.
			Attachment data = new Attachment(textMessage, MediaTypeNames.Text.Plain);
			// Send textMessage as part of the e-mail body.
			message.Attachments.Add(data);
			ContentDisposition disposition = data.ContentDisposition;
			disposition.Inline = true;
			//Send the message.
			// Include credentials if the server requires them.
			SmtpClient client = new SmtpClient(server);
			client.Credentials = CredentialCache.DefaultNetworkCredentials;

	    try {
        client.Send(message);
      }
      catch (Exception ex) {
        Console.WriteLine("Exception caught in CreateMessageInlineAttachment: {0}", 
                    ex.ToString() );
      }
			data.Dispose();
		}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Special Character limitations in HTTP Headers
If you are specifying the ContentDisposition on an HTTP response to a HTTP client, there are several limitations to what characters you can put in the file name, based on the type of client browser.  
You cannot include a semi-colon ( ; ) in the filename, or the filename will be truncated at the semi-colon.  Firefox/Mozilla truncates the filename if there is a space in the name. 

http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download

See "5.1. Syntax of the Content-Type Header Field" in RFC 2045 for the list of special characters that must be enclosed in double quotes ( " ) when specified in the filename.

http://www.ietf.org/rfc/rfc2045.txt

This VB.NET code will cause most file names to pass through to the web browser correctly (tested with IE 8).  The filename variable cannot be HTML encoded or URL encoded:

Response.AppendHeader("Content-Disposition", "inline; filename=""" + filename.Replace(";", "%3B").Replace(" ", "%20") + """")