Click here to Skip to main content
6,822,613 members and growing! (20,452 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Common Public License Version 1.0 (CPL)

Sending Compressed HTTP Reponse in ASP.NET Web Application

By Maxim Novak

Shows how to use the Response.Filter to send a compressed response in an ASP.NET web application
C# (C#3.0), HTML, Windows, .NET (.NET3.5), ASP.NET, Architect, Dev
Revision:4 (See All)
Posted:31 Oct 2009
Updated:6 Nov 2009
Views:3,731
Bookmarked:29 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 4.09 Rating: 4.29 out of 5

1

2
1 vote, 11.1%
3
3 votes, 33.3%
4
5 votes, 55.6%
5
Sample Image - maximum width is 600 pixels

Introduction

This article will show you how to send a compressed HTTP response.

This will work on any file or response that you're sending but we'll try to avoid compressing very small files or files that are already compressed (like .jpeg)

Background

At my work, I have to send big XML and CSV files to the user. These files can be compressed to much smaller sizes.

One solution is to save the XML as a file in a temporary location on the server, zip it, and then send it to the user.

In this article, I'll show a much better solution for the problem. We'll send the XML as a compressed stream directly to the browser.

Using the Code

This code line will make sure that the browser will open the save file dialog when sending the file:

Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.xml");

Note that if you don't want the user to download a file, just send the response compress to avoid using this line of code.

The main part of the is code in the SetResponseCompression() method.

First we have to check that the browser supports compression. We'll check the 'Accept-Encoding' header in the request.

A typical header looks like "gzip,deflate".

string acceptEncoding = Request.Headers["Accept-Encoding"].ToLower();
    if (acceptEncoding.Contains("gzip"))
	{ .... 

The next lines set the content encoding to 'gzip' and sets a GZipStream as the Response Filter.

This way the response will be sent as GZip stream and the header will say to the browser that this is a GZip and the browser will know how to open it.

Response.AppendHeader("Content-Encoding", "gzip");
Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);

You can use SetResponseCompression() in any page. This will make the response sent compressed.

History

  • 31st October, 2009: Initial version

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)

About the Author

Maxim Novak


Member

Occupation: Software Developer
Company: SuperDerivatives
Location: Israel Israel

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 6 Nov 2009
Editor: Deeksha Shenoy
Copyright 2009 by Maxim Novak
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project