Any inaccuracies in this index may be explained by the fact that it has been sorted with the help of a computer. --Donald Knuth
Welcome to my blog and project site for Microsoft.NET development.

I've been a full time .NET developer for ten years, but I didn't start my professional life as a programmer ... more
Share/Print this page:

How to Deliver a File From ASP.NET and Specify Its MIME Type

The Role of MIME in HTTP Communications

By steve on February 15, 2007.
Updated on January 22, 2012.
Viewed 8,271 times (0 times today).
Article TypesArticle TypesTechnologiesTopicsTopics
ReferenceSnippetASP.NETPolicy and StandardsWeb

Summary

Contents

What does MIME stand for, how does it relate to HTTP messages and why should I, as a web developer, care?

What is MIME?

Contents

MIME is an acronym meaning Multipurpose Internet Mail Extensions.

Originally developed in 1992, MIME was a standard created to allow the exchange of non-ASCII content over email.

Email was, and is really, an ASCII medium. MIME provides a mechanism for attaching data of non-ASCII types like images, sound files, multi-part messages, video, etc.

The key feature of a MIME encoded message is that it indicates the content-type of the encoded data.

I use the word encoded, because email was a pure ASCII medium and in order to transfer non-ASCII content, the binary data was encoded as a series of ASCII characters which could then be decoded on the other side.

As long as the email program receiving the data knows what kind of file it is, it can try to do something appropriate with the data.

All MIME headers indicate the content-type of the contained data.

HTTP and MIME

Contents

HTTP uses MIME-like messages to transfer content. I say MIME-like because HTTP does not purport to be MIME-compliant even though it may borrow MIME concepts and HTTP messages may be in full compliance with the MIME protocol.

MIME-like headers serve to indicate the type of data contained in the message, thus allowing the receiver to decode and interpret the data and ultimately act on the data by displaying it or prompting the user for some action.

The official W3 specs for HTTP says this about the relationship between MIME and HTTP:

   "The HTTP protocol is a request/response protocol. A client sends a
   request to the server in the form of a request method, URI, and
   protocol version, followed by a MIME-like message containing request
   modifiers, client information, and possible body content over a
   connection with a server. The server responds with a status line,
   including the message's protocol version and a success or error code,
   followed by a MIME-like message containing server information, entity
   metainformation, and possible entity-body content."

The Role of MIME-types in HTTP Responses

Contents

So, as a web developer, if you are generating an HTTP response that will be returned to a browser or other HTTP client, you will want a way to tell the browser what type of data is contained in the message you are sending. The standard MIME-type name (ie "text/plain", "image/jpeg", "application/pdf" ... ) is sent in the HTTP header to do just this.

The following C# code sample uses MIME types to indicate the the file type being returned in the HTTP response:

Deliver File from HTTP Server and Specify Its Mime-type

Contents
public void DeliverJpegFile(byte[] bytes, string fileName)

	Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
	Response.ContentType = image/jpeg"; // this is where we indicate the MIME-type
	Response.OutputStream.Write(bytes, 0, bytes.Length);
	Response.End();

}
Back to Top

User Comments (0)

Post Your Comment
  You may post without logging in or login here.
Display Name: Required.
Email: Required. Will not be shown. Used for identicon.
Comment:
Allowed tags: <quote></quote>, <code></code>, <b></b>, <i></i>, <u></u>, <red></red>
 
   Please type text as shown in the image at left.