Chapter 15. File Operations

Introduction

Downloading a file from a server is a fairly common requirement of web applications, whereas uploading a file is less so. For example, you might want your application to allow users to download PDF, binary, or image files to their browsers. Alternatively, you might have a content management system to which you want to let users upload images or binary files.

ASP.NET provides an easy-to-use and flexible infrastructure you can use to complete either task. You can easily download a file to the browser for display, storage, or printing by streaming it to the Response object, a fairly simple task in ASP.NET.

Uploading a file to the web server for storage is also easy, but you need to know a few tricks about how to set up an Upload button and how to encode the Form element for the page that sends user input to the server.

Sometimes you may want to upload a file to the server for processing only, without storing it there, and at other times you may need to store the contents of an uploaded file to a database. For example, you might want to do the former to avoid having to deal with problems associated with files being uploaded with the same names, inadvertently filling the hard drive, or allowing the ASPNET write privileges on the local filesystem. Storing an uploaded file in a database is useful when you want to keep a complete record of the file set apart from the web server’s filesystem. The recipes in this chapter show you how to do all of these ...

Get ASP.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.