Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This article creates a sample page that demonstrates how to use C# to retrieve binary data from a file and then write the data out to the browser. Although this demonstration uses an Adobe Acrobat (.pdf) file, you can apply this procedure to other binary file formats.
Original product version: ASP.NET
Original KB number: 306654
This section demonstrates how to create a new ASP.NET web application named BinaryDemo:
http://localhost
.To set up your project so that you can add and run the code in the Create the ASPX page section, you must first add an Adobe Acrobat (.pdf) file to your current project.
To add the PDF file to the project in Visual Studio, follow these steps:
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 Adobe Web site.
Add a new .aspx page named BinaryData.aspx to the current project as follows:
In Solution Explorer, right-click the project node, select Add > New Item > Web Form.
Name the page BinaryData.aspx, and then select Add.
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 Solution Explorer, right-click BinaryData.aspx, and then select View Code.
Highlight the following code, right-click the code, and then select Copy. In the Page_Load
event on the code-behind page, select 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.
Response.WriteFile(FilePath);
Response.End();
}
On the File menu, select Save All.
On the Build menu, select Build.
To run the code, right-click BinaryData.aspx in Solution Explorer, and then select View In Browser. If you're prompted, select Open to open and render the file in the browser.
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 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 Word files)Application/x-msexcel
(for Excel files)For more information, visit below sites:
For a Visual Basic .NET version of this article, see Write binary files to the browser by using ASP.NET and Visual Basic .NET.
Third-party information disclaimer
The third-party products that this article discusses are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, about the performance or reliability of these products.
Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.