File Uploading to Server Hard Disk using ASP.NETby Faisal Khan.
Overview
In this article we will learn how to upload one or more files from the client browser to
the server hard disk using plain ASP.NET. We will not be creating or making use of any
commercial or home-made components, instead we will learn to make use of standard ASP.NET
HTML controls which are part of ASP.NET 1.0.
File Uploading Using Plain ASP
If you are looking to upload files using plain ASP ( VBScript ) then here are few links
to popular Stardeveloper articles on how to do it:
- File uploading
to server hard disk.
- Uploading files
( binary data ) to the database.
- Displaying
files ( binary data ) from the database.
There were quite a few things users have been asking about which weren't covered in those
articles like how to make correct file name appear in the 'Save As' dialogue box once the user
tries to save any file instead of a generic 'file.asp' appearing? and how to make user download a
file without viewing it?
File Uploading Using ASP.NET
Here are few Stardeveloper articles on this topic:
- File uploading
to server hard disk ( this article ).
- File uploading to Microsoft Access database.
- Uploading images, determining size, width & height and resizing image files.
Advantages of ASP.NET File Uploading over Plain ASP
Classic ASP operates in a scripting environment which allows access to raw bytes only at a
certain level and one has to hack one's way in order to do that. File uploading is the same. You
have to move and copy hell lot of bytes here and there to do that and even when you do it, it takes a
lot of server time.
ASP.NET on the other hands operates in a full programming environment and lets the developers
work with bits and bytes as they like. Not only ASP.NET pages are faster than classic ASP ones, but
they also come with a pre-built Class library developed by top Microsoft developers to provide you
with more functionality and options than you have ever imagined with classic ASP.
One namespace among those hierarchy of classes contains classes for creating HTML server
controls for ASP.NET pages. One of the controls among them is System.Web.UI.HtmlControls.HtmlInputFile
control. This single controls makes our task of uploading files a breeze. This control is very fast and
as we'll see in a moment, it allows you to upload one or more files of any size in no time.
So if you take my advice and have the privilege of running ASP.NET pages on your
internet/intranet then do yourself a favor and go for file uploading with ASP.NET. You are going to
save yourself lot of trouble by doing that and in return you will not have to pay anything and your
application will work the same everywhere ( where ASP.NET pages can be run ).
Besides I don't see any Windows hosting provider not offering ASP.NET. So there is no
reason for you not to make use of ASP.NET file uploading. Just go for it and enjoy the rest
of the article.
Problem
Let's first consider what we want to do. We want to:
- Upload one or more files to server hard disk.
- Allow the user to accept only certain MIME file types like images etc. It is useful
especially when you only want the user to upload say image files like jpeg, gif, png.
- Allow the user to select the files and leave the file input boxes empty if he/she doesn't
want to upload. We should handle this situation gracefully.
- Also provide other form options like input boxes for first and last names, favorite
programming languages etc so that we learn how to receive regular form post data along with
binary file data at the same time.
- Allow the user to view uploaded file attributes like file name and size without actually
saving it on the server hard disk.
- Allow the user to select a folder on the server to upload files to.
- View all the uploaded files in all the folders available for file upload.
- When viewing files also display content-type of the file by taking default content-type for
a given file extension from the system registry. It can be very useful when viewing files from
the server so that server can send correct content-type to the client browser.
- Allow the user to view the file online.
- Allow the user to be forced to download file. We'll see how this is done.
- Delete uploaded files.
- Set maximum file upload size limit.
Alright, we've got a pretty huge problem list. Let's see how we design our solution. Overview
In this article we will learn how to upload one or more files from the client browser to
the server hard disk using plain ASP.NET. We will not be creating or making use of any
commercial or home-made components, instead we will learn to make use of standard ASP.NET
HTML controls which are part of ASP.NET 1.0.
File Uploading Using Plain ASP
If you are looking to upload files using plain ASP ( VBScript ) then here are few links
to popular Stardeveloper articles on how to do it:
- File uploading
to server hard disk.
- Uploading files
( binary data ) to the database.
- Displaying
files ( binary data ) from the database.
There were quite a few things users have been asking about which weren't covered in those
articles like how to make correct file name appear in the 'Save As' dialogue box once the user
tries to save any file instead of a generic 'file.asp' appearing? and how to make user download a
file without viewing it?
File Uploading Using ASP.NET
Here are few Stardeveloper articles on this topic:
- File uploading
to server hard disk ( this article ).
- File uploading to Microsoft Access database.
- Uploading images, determining size, width & height and resizing image files.
Advantages of ASP.NET File Uploading over Plain ASP
Classic ASP operates in a scripting environment which allows access to raw bytes only at a
certain level and one has to hack one's way in order to do that. File uploading is the same. You
have to move and copy hell lot of bytes here and there to do that and even when you do it, it takes a
lot of server time.
ASP.NET on the other hands operates in a full programming environment and lets the developers
work with bits and bytes as they like. Not only ASP.NET pages are faster than classic ASP ones, but
they also come with a pre-built Class library developed by top Microsoft developers to provide you
with more functionality and options than you have ever imagined with classic ASP.
One namespace among those hierarchy of classes contains classes for creating HTML server
controls for ASP.NET pages. One of the controls among them is System.Web.UI.HtmlControls.HtmlInputFile
control. This single controls makes our task of uploading files a breeze. This control is very fast and
as we'll see in a moment, it allows you to upload one or more files of any size in no time.
So if you take my advice and have the privilege of running ASP.NET pages on your
internet/intranet then do yourself a favor and go for file uploading with ASP.NET. You are going to
save yourself lot of trouble by doing that and in return you will not have to pay anything and your
application will work the same everywhere ( where ASP.NET pages can be run ).
Besides I don't see any Windows hosting provider not offering ASP.NET. So there is no
reason for you not to make use of ASP.NET file uploading. Just go for it and enjoy the rest
of the article.
Problem
Let's first consider what we want to do. We want to:
- Upload one or more files to server hard disk.
- Allow the user to accept only certain MIME file types like images etc. It is useful
especially when you only want the user to upload say image files like jpeg, gif, png.
- Allow the user to select the files and leave the file input boxes empty if he/she doesn't
want to upload. We should handle this situation gracefully.
- Also provide other form options like input boxes for first and last names, favorite
programming languages etc so that we learn how to receive regular form post data along with
binary file data at the same time.
- Allow the user to view uploaded file attributes like file name and size without actually
saving it on the server hard disk.
- Allow the user to select a folder on the server to upload files to.
- View all the uploaded files in all the folders available for file upload.
- When viewing files also display content-type of the file by taking default content-type for
a given file extension from the system registry. It can be very useful when viewing files from
the server so that server can send correct content-type to the client browser.
- Allow the user to view the file online.
- Allow the user to be forced to download file. We'll see how this is done.
- Delete uploaded files.
- Set maximum file upload size limit.
Alright, we've got a pretty huge problem list. Let's see how we design our solution.
|