How To Write Binary Files to the Browser Using ASP.NET and Visual C# .NET


This step-by-step article creates a sample page that demonstrates how to retrieve binary data from a file and then how to write the data out to the browser using ASP.NET and Visual C#. Although this demonstration uses an Adobe Acrobat (.pdf) file (which Web application developers commonly use), you can apply this procedure to other binary file formats.


  • Microsoft .NET Framework
  • Microsoft Windows 2000 or Windows XP
  • Microsoft Internet Information Server (IIS)

Create an ASP.NET Web Application Using Visual C#

This section demonstrates how to create a new ASP.NET Web Application named BinaryDemo:
  1. Open Microsoft Visual Studio .NET
  2. From the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C# Projects. Under Templates, click ASP.NET Web Application.
  4. In the Name text box, type BinaryDemo. In the Location text box, type the server name. If you are using the local server, leave the location as http://localhost.

Add the PDF File to the Project

To set up your project so that you can add and run the code in the Create an ASPX Page section to follow, you must first add an Adobe Acrobat (.pdf) file to your current project. To do this in Visual Studio .NET, follow these steps:
  1. In Solution Explorer, right-click the project node, click Add, and then click Add Existing Item.
  2. Browse to the location of a .pdf file on your system.
  3. Click to highlight the file, and then click Open.
  4. In Visual Studio .NET Solution Explorer, right-click the file, and then click Rename. Rename the .pdf file so that it matches the file name Acrobat.pdf that is used in the code that follows.
In addition, ensure that Adobe Acrobat Reader is installed on the client computer from which the .aspx page is viewed so that the browser can properly read and render the binary data. You can download the Adobe Acrobat Reader from the following Adobe Web site:

Create the ASPX Page

  1. Add a new .aspx page named BinaryData.aspx to the current project as follows:
    1. In Solution Explorer, right-click the project node, click Add, and then click Add Web Form.
    2. Name the page BinaryData.aspx, and then click Open.

      NOTE: Make sure that your page is added to the project at the same level as the .pdf file that you added in the previous section. This is very important because the code uses the relative path to initially reference the .pdf file.
  2. In the editor, right-click BinaryData.aspx, and then click View Code.
  3. Highlight the following code, right-click the code, and then click Copy. In the Page_Load event in the code-behind page, click Paste on the Edit menu to paste the code:
    private void Page_Load(object sender, System.EventArgs e)
    //Set the appropriate ContentType.
    Response.ContentType = "Application/pdf";
    //Get the physical path to the file.
    string FilePath = MapPath("acrobat.pdf");
    //Write the file directly to the HTTP content output stream.
  4. On the File menu, click Save All.
  5. On the Build menu, click Build.
  6. To run the code, right-click BinaryData.aspx in Solution Explorer, and then click View In Browser. If you are prompted, click Open to open and render the file in the browser.
NOTE: If you want to use the preceding code to support other binary file types, you must modify the value in the ContentType string so that it specifies the appropriate file format. The syntax of this string is usually formatted as "type/subtype," where "type" is the general content category and "subtype" is the specific content type. For a full list of supported content types, refer to your Web browser documentation or the current HTTP specification. The following list outlines some common ContentType values:
  • "text/HTML"
  • "image/GIF"
  • "image/JPEG"
  • "text/plain"
  • "Application/msword" (for Microsoft Word files)
  • "Application/x-msexcel" (for Microsoft Excel files)


For more information, refer to the following topics in the .NET Framework Software Development Kit (SDK) documentation: For additional information about how to accomplish this task using Microsoft Active Server Pages (ASP), click the article number below to view the article in the Microsoft Knowledge Base:
276488 How To Use the ADODB.Stream Object to Send Binary Files to the Browser through ASP
Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

The third-party products that are discussed in this article are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, regarding the performance or reliability of these products.


ID articol: 306654 - Ultima examinare: 12 iun. 2012 - Revizie: 1