Home > 2010 Winter BTI420 > Uploading files to varbinary(max) and varchar(max) destinations

Uploading files to varbinary(max) and varchar(max) destinations


Recently, you learned how to upload files to a varbinary(max) destination. We’ll restate the technique here, and cover a related topic, uploading to a varchar(max) destination.

Both of these techniques enable a browser user to upload digital content to a database, by using data access components (a data access layer / DataSet).
.

Upload to varbinary(max)

Use this technique when uploading digital content that is intended for use as a binary digital stream. It is suitable for images (e.g. jpg, png, etc.), and other media (e.g. mp3, wmv, etc.).

It turns out that this technique is very simple. The ASP.NET File Upload web server control exposes a FileBytes property as a byte array. This can be assigned directly to a table adapter argument. Database varbinary(max) columns are exposed as byte arrays to C# code.
.

Upload to varchar(max)

Use this technique when uploading digital content that is intended for use as a character or byte oriented stream. It is suitable for text content (e.g. txt, html, xml, etc.).

The ASP.NET File Upload web server control does NOT have a property that exposes the uploaded file as a string. Therefore, we must “decode” the byte array into a string.

To do this, we take advantage of the long-established “best practice” that recommends that web, email, and general text/character-oriented content use a “UTF-8” encoding. There is a .NET Framework class that will decode a byte array, using the UTF-8 encoding, into a string.

A table adapter argument for a varchar(max) column is a string type. To create a string from a file upload control’s uploaded file, the following code will work:

// Assume that “fileUploader” is the ID (instance name)
// of an ASP.NET File Upload web server control…
string s = System.Text.UTF8Encoding.GetString(fileUploader.FileBytes);

.


Advertisements
Categories: 2010 Winter BTI420
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: