Home | Demo | Contact Us | Download | Support | Purchase | Products


Other Products
 
  "[In response to a support answer]Check that, I got it. :-) You guys are the best!"
Austin Castro
 
More
Testimonials

Send an Attachment to the Browser

The following example demonstrates streaming an attachment to the browser.

If an attachment is found, the attachment is checked to see if it is an image. If it is an image, then it is streamed directly to the client and should directly show up in the browser.

 

This is accomplished by the code

[C#]

Response.ContentType = attachment.ContentType.Value;
attachment.WriteToStream( Response.OutputStream );

 

[Visual Basic]

Response.ContentType = attachment.ContentType.Value
attachment.WriteToStream(Response.OutputStream)
 

 

If the attachment isn't an image, the "Save File As" dialog is presented to the client. This is achieved by the following code:

[C#]

Response.ContentType = "APPLICATION/OCTET-STREAM";
//set the filename
Response.AddHeader( "Content-Disposition", "attachment;filename=\"" + attachment.AttachmentName() + "\"" );
attachment.WriteToStream( Response.OutputStream );
 

[Visual Basic]

'set the Content-Type as an attachment
Response.ContentType = "APPLICATION/OCTET-STREAM"
 
'set the filename
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + attachment.AttachmentName() + "\"")
attachment.WriteToStream(Response.OutputStream)
 

 

 

The complete ASP.NET .aspx pages can be found below for both C# and Visual Basic. Please note that although these are ASP.NET pages, there ISN'T any HTML content in the pages. This is because the pages are actually streaming binary content to the browser.

[C#]

<%@ Page language="c#" %>
<%@ Import Namespace="aspNetMime"%>
<%@ Import Namespace="System.IO"%>

<script runat=server>
		
		//notice that even though this is an ASP.NET page there ISN'T any HTML code on this page.
		//that's because this page is meant to send binay content (the attachment) to the browser
			
		private void Page_Load(object sender, System.EventArgs e)
		{

			//an email on the filesystem
			string filename = "testEmail.eml";

			//open the email
			StreamReader sr = new StreamReader( Server.MapPath( filename ) );
			string emailContents = sr.ReadToEnd();
			sr.Close();

			//this creates a Message from a stream
			aspNetMime.MimeMessage msg = new aspNetMime.MimeMessage( emailContents );

			//send the first attachment to the browser
			if( msg.Attachments.Count > 0 )
			{
				//get the first attachment
				MimePart attachment = msg.Attachments[0];

				//check to see if the attachment has a Content-Type, and if the Content-Type is an image
				//if it is an image, then stream it directly to the brower.
				if( ( attachment.ContentType != null ) && ( attachment.ContentType.Value.IndexOf( "image" )>-1 ) )
				{
					Response.ContentType = attachment.ContentType.Value;
					attachment.WriteToStream( Response.OutputStream );

				}
				else 
				{
					//this method should open the "Save File As" dialog box in the browser
					
					//set the Content-Type as an attachment
					Response.ContentType = "APPLICATION/OCTET-STREAM";
					
					//set the filename
					Response.AddHeader( "Content-Disposition", "attachment;filename=\"" + attachment.AttachmentName() + "\"" );
					attachment.WriteToStream( Response.OutputStream );

				}
			}


		}

</script>

 

[VB.NET]

<%@ Page language="vb" %>
<%@ Import Namespace="aspNetMime"%>
<%@ Import Namespace="System.IO"%>

<script runat=server>
		
		'notice that even though this is an ASP.NET page there ISN'T any HTML code on this page.
		'that's because this page is meant to send binay content (the attachment) to the browser
 
		Private  Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 
			'an email on the filesystem
			Dim filename As String =  "testEmail.eml" 
 
			'open the email
			Dim sr As StreamReader =  New StreamReader(Server.MapPath(filename)) 
			Dim emailContents As String =  sr.ReadToEnd() 
			sr.Close()
 
			'this creates a Message from a stream
			Dim msg As aspNetMime.MimeMessage =  New aspNetMime.MimeMessage(emailContents) 
 
			'send the first attachment to the browser
			If msg.Attachments.Count > 0 Then
				'get the first attachment
				Dim attachment As MimePart =  msg.Attachments(0) 
 
				'check to see if the attachment has a Content-Type, and if the Content-Type is an image
				'if it is an image, then stream it directly to the brower.
				If (Not attachment.ContentType Is Nothing)  AND (attachment.ContentType.Value.IndexOf("image")>-1) Then
					Response.ContentType = attachment.ContentType.Value
					attachment.WriteToStream(Response.OutputStream)
 
				Else 
					'this method should open the "Save File As" dialog box in the browser
 
					'set the Content-Type as an attachment
					Response.ContentType = "APPLICATION/OCTET-STREAM"
 
					'set the filename
					Response.AddHeader("Content-Disposition", "attachment;filename=\"" + attachment.AttachmentName() + "\"")
					attachment.WriteToStream(Response.OutputStream)
 
				End If
			End If
 
 
		End Sub

</script>


 

 

 



The box is not shipped. aspNetMime is a downloadable product.