Microsoft KB Archive/307603

= How To Write Binary Files to the Browser Using ASP.NET and Visual Basic .NET =

Article ID: 307603

Article Last Modified on 11/8/2006

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft ASP.NET 1.1
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q307603





For a Microsoft Visual C# .NET version of this article, see 306654.

IN THIS TASK
SUMMARY
 * Requirements
 * Create an ASP.NET Application Using Visual Basic
 * Add a PDF File to the Project
 * Create an ASPX Page

REFERENCES



SUMMARY
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 Basic .NET. 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.

back to the top

Requirements

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

back to the top

Create an ASP.NET Web Application Using Visual Basic .NET
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 Basic 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.

back to the top

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:

http://www.adobe.com

back to the top

Create the ASPX Page
 Add a new .aspx page named BinaryData.aspx to the current project as follows:  In Solution Explorer, right-click the project node, click Add, and then click Add Web Form. 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.  In the editor, right-click BinaryData.aspx, and then click View Code.  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 Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Set the appropriate ContentType. Response.ContentType = &quot;Application/pdf&quot; 'Get the physical path to the file. Dim FilePath As String = MapPath(&quot;acrobat.pdf&quot;) 'Write the file directly to the HTTP output stream. Response.WriteFile(FilePath) Response.End End Sub </li> On the File menu, click Save All.</li> On the Build menu, click Build.</li> 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.</li></ol>

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 &quot;type/subtype,&quot; where &quot;type&quot; is the general content category and &quot;subtype&quot; 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:
 * &quot;text/HTML&quot;
 * &quot;image/GIF&quot;
 * &quot;image/JPEG&quot;
 * &quot;text/plain&quot;
 * &quot;Application/msword&quot; (for Microsoft Word files)
 * &quot;Application/x-msexcel&quot; (for Microsoft Excel files)

back to the top

<div class="references_section">