现在的位置: 首页 > 综合 > 正文

File Uploading to Server Hard Disk using ASP.NET

2012年07月07日 ⁄ 综合 ⁄ 共 3765字 ⁄ 字号 评论关闭
http://www.stardeveloper.com/articles/display.html?article=2003022601&page=1

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.

There are lots of commercial ASP COM components available to upload files at quick speed but then they are not free. Couple of them which come to mind are:

  1. AspUpload.
  2. SA's FileUp Server Control.

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.

抱歉!评论已关闭.