Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : File Uploading to Server Hard Disk using ASP.NET
 

File Uploading to Server Hard Disk using ASP.NET

by Faisal Khan.Follow Faisal Khan on Twitter

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:

  1. File uploading to server hard disk.
  2. Uploading files ( binary data ) to the database.
  3. 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:

  1. File uploading to server hard disk ( this article ).
  2. File uploading to Microsoft Access database.
  3. 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:

  1. Upload one or more files to server hard disk.
  2. 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.
  3. 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.
  4. 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.
  5. Allow the user to view uploaded file attributes like file name and size without actually saving it on the server hard disk.
  6. Allow the user to select a folder on the server to upload files to.
  7. View all the uploaded files in all the folders available for file upload.
  8. 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.
  9. Allow the user to view the file online.
  10. Allow the user to be forced to download file. We'll see how this is done.
  11. Delete uploaded files.
  12. 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:

  1. File uploading to server hard disk.
  2. Uploading files ( binary data ) to the database.
  3. 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:

  1. File uploading to server hard disk ( this article ).
  2. File uploading to Microsoft Access database.
  3. 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:

  1. Upload one or more files to server hard disk.
  2. 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.
  3. 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.
  4. 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.
  5. Allow the user to view uploaded file attributes like file name and size without actually saving it on the server hard disk.
  6. Allow the user to select a folder on the server to upload files to.
  7. View all the uploaded files in all the folders available for file upload.
  8. 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.
  9. Allow the user to view the file online.
  10. Allow the user to be forced to download file. We'll see how this is done.
  11. Delete uploaded files.
  12. Set maximum file upload size limit.

Alright, we've got a pretty huge problem list. Let's see how we design our solution.


 ( 12 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 30, Comments: 51 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. i am having a problem and i think u can solve it
  2. Stardeveloper Error
  3. Uploading
  4. How to handle huge file uploads? ( 1 Reply )
  5. user authentication to upload files
  6. ERROR: Picture Processor Server Component is not installed!
  7. how to save file (stored on SQL server) to file/web server?
  8. Need Help Please
  9. file upload error ( 1 Reply )
  10. Saving table Contents in ASP.NET
  11. Table ID Value
  12. How to use com components in ASP.net
  13. Access to the path ( 1 Reply )
  14. When uploading the images is there a way to display a progress bar?
  15. Problem in viewing a file in browser?
  16. How to insert data into table have foreign key other table used store file upload
  17. Parser Error Message: Could not load type Stardeveloper.UploadAccess.UploadForm ( 2 Replies )
  18. Unable to download code
  19. I have problem
  20. i need to load pdf files only... help!!!
  21. get permission
  22. File SaveAs overwrites existing file of same name ( 1 Reply )
  23. can't save to server hard disk ( 1 Reply )
  24. Great Article
  25. batch file didn't work ( 1 Reply )
  26. File Uploading to Server Hard Disk using ASP.NET ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  27. How to display the error information when the size ...? ( 6 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  28. Could not load type 'Stardeveloper.UploadHardDisk.DefaultForm' ( 6 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  29. Stuff not working (Clarification needed)
  30. song

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.