Sie sind auf Seite 1von 16

6/20/13

Uploading Files Using the File Field Control

Uploading Files Using the File Field Control


75 out of 104 rated this helpful By Bill Evjen Reuters Feburary 2004 Applies to: Microsoft ASP.NET Summary: Learn how to use the Microsoft ASP.NET File Field control to allow your end-users to upload one or more files to your server. (16 printed pages)

Contents
Introduction Looking Closely at the File Field Control Working Around File Size Limitations Controlling Uploaded File Types Uploading Multiple Files at the Same Time Conclusion Related Books

Introduction
Microsoft ASP.NET Web Forms are all about communicating with an end-user to abstract what information you need to from them depending on the service administered. In many cases with Web Forms, it is usually simply textual data that is collected and stored. However, there are many cases where you are going to need more than simple textual data; you may want your users to upload files to the server. For instance, your application can have a document library that is shared with all users. There are plenty of examples, but invariably you will need this ability to upload any type of document for storage, or to have your application take action upon the document once it is uploaded. In any case, ASP.NET provides you with a simple ability to upload documents to the server with just a little work on your part. This article describes how to use the File Field control for uploading documents to your Web server.

msdn.microsoft.com/en-us/library/aa478971.aspx

Looking Closely at the File Field Control

1/16

6/20/13

Uploading Files Using the File Field Control

Control

The File Field control uses the H t m l I n p u t F i l eclass and is a cool control simply because it does things that were difficult to do before ASP.NET (that is, without buying a third-party component to do it for you). The File Field control uploads files to the server from the client's machine. It is important to note that this control is called the File Field control in all versions of Microsoft Visual Studio .NET, while in the ASP.NET Web Matrix, this control is called the FileUpload control. In both cases, you will find the control in the HTML section of the Toolbox. The File Field control allows access to program the HTML < i n p u tt y p e = " f i l e " >tag. This tag is used to work with file data within an HTML form. In the past when using classic ASP (ASP 3.0 or earlier), many programmers worked with third-party components to upload files from the client to the server. Now, with .NET and this new control, uploading is taken care of for youand it couldn't be simpler. Listing 1 shows you how to use the File Field control to upload files to the server. Note The sample code is provided in both Microsoft Visual Basic (VB) and C#. Listing 1. Uploading files to the server using the File Field control VB < % @P a g eL a n g u a g e = " V B "% > < s c r i p tr u n a t = " s e r v e r " > S u bS u b m i t B u t t o n _ C l i c k ( S o u r c eA sO b j e c t ,eA s E v e n t A r g s ) I fN o t( F i l e 1 . P o s t e d F i l eI sN o t h i n g )T h e n T r y F i l e 1 . P o s t e d F i l e . S a v e A s ( " C : \ U p l o a d s \ u p l o a d e d f i l e . t x t " ) S p a n 1 . I n n e r H t m l=" U p l o a dS u c c e s s f u l ! " C a t c he xA sE x c e p t i o n S p a n 1 . I n n e r H t m l=" E r r o rs a v i n gf i l e < b > C : \ \ "&_ F i l e 1 . V a l u e&" < / b > < b r > "&e x . T o S t r i n g ( ) E n dT r y E n dI f E n dS u b < / s c r i p t > < h t m l > < h e a d > < / h e a d > < b o d y >

msdn.microsoft.com/en-us/library/aa478971.aspx

2/16

6/20/13

Uploading Files Using the File Field Control

< b o d y > < f o r mr u n a t = " s e r v e r "e n c t y p e = " m u l t i p a r t / f o r m d a t a " > S e l e c taf i l et ou p l o a d : < b r/ > < i n p u tt y p e = " f i l e "i d = " F i l e 1 "r u n a t = " S e r v e r " > < p > < i n p u tt y p e = " s u b m i t "i d = " S u b m i t 1 "r u n a t = " S e r v e r " v a l u e = " U p l o a dF i l e "O n S e r v e r C l i c k = " S u b m i t B u t t o n _ C l i c k " > < p > < s p a ni d = " S p a n 1 "r u n a t = " S e r v e r "/ > < / f o r m > < / b o d y > < / h t m l > C# < % @P a g eL a n g u a g e = " C # "% > < s c r i p tr u n a t = " s e r v e r " > v o i dS u b m i t B u t t o n _ C l i c k ( O b j e c ts e n d e r ,E v e n t A r g se ){ i f( F i l e 1 . P o s t e d F i l e! =n u l l ){ t r y{ F i l e 1 . P o s t e d F i l e . S a v e A s ( " C : \ \ U p l o a d s \ \ u p l o a d e d f i l e . t x t " ) ; S p a n 1 . I n n e r H t m l=" U p l o a dS u c c e s s f u l ! " ; } c a t c h( E x c e p t i o ne x ){ S p a n 1 . I n n e r H t m l=" E r r o rs a v i n gf i l e < b > C : \ \ "+ F i l e 1 . V a l u e+" < / b > < b r > "+e x . T o S t r i n g ( ) ; } } } < / s c r i p t > < h t m l > < h e a d > < / h e a d > < b o d y > < f o r mr u n a t = " s e r v e r "e n c t y p e = " m u l t i p a r t / f o r m d a t a " > S e l e c taf i l et ou p l o a d : < b r/ > < i n p u tt y p e = " f i l e "i d = " F i l e 1 "r u n a t = " S e r v e r " > < p > < i n p u tt y p e = " s u b m i t "i d = " S u b m i t 1 "r u n a t = " S e r v e r " v a l u e = " U p l o a dF i l e "O n S e r v e r C l i c k = " S u b m i t B u t t o n _ C l i c k " > < p > < s p a ni d = " S p a n 1 "r u n a t = " S e r v e r "/ > < / f o r m >

msdn.microsoft.com/en-us/library/aa478971.aspx

3/16

6/20/13

Uploading Files Using the File Field Control

< / f o r m > < / b o d y > < / h t m l >

When the page from Listing 1 is run, you can select a file and upload it to the server by clicking the Upload File button on the page. This is a great feature that ASP 3.0 programmers always wished for. Now it is here in .NET! With only a few lines of code, it is easy to upload any type of files to the server. There are some important items we should go over for this example so you understand all the needed pieces to make this work. First, for the example in Listing 1 to work, you have to make the destination folder on the server writeable for the account used by ASP.NET so the file can be saved to the specified folder. If you think your ASP.NET account is not enabled to write to the folder you want, simply open up Microsoft Windows Explorer and navigate to the folder to which you want to add this permission. Right-click on the folder (in this case, the Uploads folder), and then select Properties. In the Properties dialog box, click on the Security tab and make sure the ASP.NET Machine Account is included in the list and has the proper permissions to write to disk (see Figure 1).

msdn.microsoft.com/en-us/library/aa478971.aspx

Figure 1. Looking at the Security tab of the Uploads folder

4/16

6/20/13

Uploading Files Using the File Field Control

folder

If you don't see the ASP.NET Machine Account under the Security tab, you can add it by clicking the Add button and entering ASPNET (without the period) in the text area, as illustrated in Figure 2.

Figure 2. Adding the ASP.NET Machine Account to the folder security definition Click OK to add the ASP.NET Machine Account to the list. From here, make sure you give this account the proper permissions; then click OK and you are ready to go. Looking at the code from Listing 1, you might notice some really important things right away. First, the < f o r m >tag has been altered by the addition of another attribute. To upload files to a server, your < f o r m >tag must have the attribute e n c t y p e = " m u l t i p a r t / f o r m d a t a " . Without this attribute, the Web form cannot upload the file. The Submit button on the page causes an O n S e r v e r C l i c kevent to occur. This event uploads the file and then displays a message telling you if the upload was successful. If it was unsuccessful, the page displays an error message describing why the upload failed. By using the < i n p u tt y p e = " f i l e " >tag, the browser automatically places a Browse button next to the text field on the ASP.NET page. You don't have to program anything else for this to occur. When the end user clicks the Browse button, he can navigate through the local file system to find the file to be uploaded to the server. This is shown in Figure 3. Clicking Open will place that filename and the file's path within the text field.

msdn.microsoft.com/en-us/library/aa478971.aspx

5/16

6/20/13

Uploading Files Using the File Field Control

Figure 3. Choosing a file

Working Around File Size Limitations


You may not realize it, but there is a limit to the size of a file that can be uploaded using this technique. By default, the maximum size of a file to be uploaded to the server using the File Field control is around 4MB. You cannot upload anything that is larger than this limit. One of the great things about .NET, however, is that it usually provides a way around limitations. You can usually change the default settings that are in place. To change this size limit, you make some changes in either the m a c h i n e . c o n f i gor w e b . c o n f i gfile. In the m a c h i n e . c o n f i gfile, find a node called < h t t p R u n t i m e >that looks like the following: < h t t p R u n t i m e e x e c u t i o n T i m e o u t = " 9 0 " m a x R e q u e s t L e n g t h = " 4 0 9 6 " u s e F u l l y Q u a l i f i e d R e d i r e c t U r l = " f a l s e " m i n F r e e T h r e a d s = " 8 " m i n L o c a l R e q u e s t F r e e T h r e a d s = " 4 " a p p R e q u e s t Q u e u e L i m i t = " 1 0 0 " / > A lot is going on in this single node, but the setting that takes care of the size of the files to be uploaded is the m a x R e q u e s t L e n g t hattribute. By default, this is set to 4096 kilobytes (KB). Simply change this value to increase the size of the files that you can upload to the server. If you want to allow 10 megabyte (MB) files to be uploaded to the server, set the m a x R e q u e s t L e n g t hvalue to 1 1 2 6 4 , meaning that the application allows files that are up to 11000 KB to be uploaded to the server. Making this change in the m a c h i n e . c o n f i gfile applies this setting to all the applications that are on the server. If you want to apply this to only the application you are working with, then apply this node to the w e b . c o n f i gfile of your application, overriding any setting that is in the m a c h i n e . c o n f i gfile. Make sure this node resides between the < s y s t e m . w e b >nodes in the configuration

msdn.microsoft.com/en-us/library/aa478971.aspx

6/16

6/20/13

Uploading Files Using the File Field Control

between the < s y s t e m . w e b >nodes in the configuration file. Another setting involved in the size limitation of files to be uploaded is the value given to the e x e c u t i o n T i m e o u t attribute in the < h t t p R u n t i m e >node. The value given the e x e c u t i o n T i m e o u tattribute is the number of seconds the upload is allowed to occur before being shut down by ASP.NET. If you are going to allow large files to be uploaded to the server, you are also going to want to increase this value along with the m a x R e q u e s t L e n g t hvalue. One negative with increasing the size of a file that can be uploaded is that there are hackers out there who attack servers by throwing a large number of requests at them. To guard against this, you can actually decrease the size of the files that are allowed to be uploaded; otherwise, you may find hundreds or even thousands of 10 MB requests hitting your server.

Controlling Uploaded File Types


There are a several methods you can use to control the types of files that are uploaded to the server. Unfortunately, there is no bullet-proof method to protect you from someone uploading files that would be considered malicious. You can take a few steps, however, to make this process of allowing end users to upload files a little more manageable. One nice method you can employ is to use the ASP.NET validation controls that are provided for free with ASP.NET. These controls enable you to do a regular-expression check upon the file that is being uploaded to see if the extension of the file is one you permit to be uploaded. This is ideal for browsers that allow client-side use of the validation controls because it forces the checking to be done on the client; the file is not uploaded to the server if the signature isn't one you allow. Listing 2 shows you an example of using validation controls to accomplish this task. Note The use of validation controls is not explained here. Take a look at Validating ASP.NET Server Controls for a complete explanation of validation controls and how to use them in your ASP.NET pages. Listing 2: Using validation controls to restrict the types of files uploaded to the server VB
msdn.microsoft.com/en-us/library/aa478971.aspx 7/16

6/20/13

Uploading Files Using the File Field Control

< % @P a g eL a n g u a g e = " V B "% > < s c r i p tr u n a t = " s e r v e r " > S u bS u b m i t B u t t o n _ C l i c k ( S o u r c eA sO b j e c t ,eA s E v e n t A r g s ) I fN o t( F i l e 1 . P o s t e d F i l eI sN o t h i n g )T h e n T r y F i l e 1 . P o s t e d F i l e . S a v e A s ( " C : \ U p l o a d s \ u p l o a d e d f i l e . t x t " ) S p a n 1 . I n n e r H t m l=" U p l o a dS u c c e s s f u l ! " C a t c he xA sE x c e p t i o n S p a n 1 . I n n e r H t m l=" E r r o rs a v i n gf i l e < b > C : \ \ "&_ F i l e 1 . V a l u e&" < / b > < b r > "&e x . T o S t r i n g ( ) E n dT r y E n dI f E n dS u b < / s c r i p t > < h t m l > < h e a d > < / h e a d > < b o d y > < f o r me n c t y p e = " m u l t i p a r t / f o r m d a t a "r u n a t = " s e r v e r " > < i n p u ti d = " F i l e 1 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < p > < i n p u ti d = " S u b m i t 1 "t y p e = " s u b m i t "v a l u e = " U p l o a dF i l e " r u n a t = " S e r v e r "o n s e r v e r c l i c k = " S u b m i t B u t t o n _ C l i c k "/ > < / p > < s p a ni d = " S p a n 1 "r u n a t = " S e r v e r "/ > < p > < a s p : R e g u l a r E x p r e s s i o n V a l i d a t o r i d = " R e g u l a r E x p r e s s i o n V a l i d a t o r 1 "r u n a t = " s e r v e r " E r r o r M e s s a g e = " O n l ym p 3 ,m 3 uo rm p e g f i l e sa r e a l l o w e d ! "V a l i d a t i o n E x p r e s s i o n = " ^ ( ( [ a z A Z ] : ) | ( \ \ { 2 } \ w + ) \ $ ? ) ( \ \ ( \ w [ \ w ] . * ) ) + ( . m p 3 | . M P 3 | . m p e g | . M P E G | . m 3 u | . M 3 U ) $ " C o n t r o l T o V a l i d a t e = " F i l e 1 " > < / a s p : R e g u l a r E x p r e s s i o n V a l i d a t o r > < b r/ > < a s p : R e q u i r e d F i e l d V a l i d a t o r i d = " R e q u i r e d F i e l d V a l i d a t o r 1 "r u n a t = " s e r v e r " E r r o r M e s s a g e = " T h i si sar e q u i r e df i e l d ! " C o n t r o l T o V a l i d a t e = " F i l e 1 " > < / a s p : R e q u i r e d F i e l d V a l i d a t o r >
msdn.microsoft.com/en-us/library/aa478971.aspx 8/16

6/20/13

Uploading Files Using the File Field Control

< / a s p : R e q u i r e d F i e l d V a l i d a t o r > < / p > < / f o r m > < / b o d y > < / h t m l > C# < % @P a g eL a n g u a g e = " C # "% > < s c r i p tr u n a t = " s e r v e r " > v o i dS u b m i t B u t t o n _ C l i c k ( O b j e c ts e n d e r ,E v e n t A r g se ){ i f( F i l e 1 . P o s t e d F i l e! =n u l l ){ t r y{ F i l e 1 . P o s t e d F i l e . S a v e A s ( " C : \ \ U p l o a d s \ \ u p l o a d e d f i l e . t x t " ) ; S p a n 1 . I n n e r H t m l=" U p l o a dS u c c e s s f u l ! " ; } c a t c h( E x c e p t i o ne x ){ S p a n 1 . I n n e r H t m l=" E r r o rs a v i n gf i l e < b > C : \ \ "+ F i l e 1 . V a l u e+" < / b > < b r > "+e x . T o S t r i n g ( ) ; } } } < / s c r i p t > < h t m l > < h e a d > < / h e a d > < b o d y > < f o r me n c t y p e = " m u l t i p a r t / f o r m d a t a "r u n a t = " s e r v e r " > < i n p u ti d = " F i l e 1 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < p > < i n p u ti d = " S u b m i t 1 "t y p e = " s u b m i t "v a l u e = " U p l o a dF i l e " r u n a t = " S e r v e r "o n s e r v e r c l i c k = " S u b m i t B u t t o n _ C l i c k "/ > < / p > < s p a ni d = " S p a n 1 "r u n a t = " S e r v e r "/ > < p > < a s p : R e g u l a r E x p r e s s i o n V a l i d a t o r i d = " R e g u l a r E x p r e s s i o n V a l i d a t o r 1 "r u n a t = " s e r v e r " E r r o r M e s s a g e = " O n l ym p 3 ,m 3 uo rm p e g f i l e sa r e a l l o w e d ! "V a l i d a t i o n E x p r e s s i o n = " ^ ( ( [ a z A Z ] : ) | ( \ \ { 2 } \ w + ) \ $ ? ) ( \ \ ( \ w [ \ w ] . * ) ) + ( . m p 3 | . M P 3 | . m p e g | . M P E G | . m 3 u | . M 3 U ) $ " C o n t r o l T o V a l i d a t e = " F i l e 1 " > < / a s p : R e g u l a r E x p r e s s i o n V a l i d a t o r >

msdn.microsoft.com/en-us/library/aa478971.aspx

9/16

6/20/13

Uploading Files Using the File Field Control

< b r/ > < a s p : R e q u i r e d F i e l d V a l i d a t o r i d = " R e q u i r e d F i e l d V a l i d a t o r 1 "r u n a t = " s e r v e r " E r r o r M e s s a g e = " T h i si sar e q u i r e df i e l d ! " C o n t r o l T o V a l i d a t e = " F i l e 1 " > < / a s p : R e q u i r e d F i e l d V a l i d a t o r > < / p > < / f o r m > < / b o d y > < / h t m l > This simple ASP.NET page uses validation controls so that the end user can only upload . m p 3 ,. m p e g , or . m 3 ufiles to the server. If the file type is not one these three choices, a Validation control throws an exception onto the screen. This is shown in Figure 4.

Figure 4. Validating the file type using validation controls Using validation controls is not a foolproof way of controlling the files that are uploaded to the server. It wouldn't be too hard for someone to change the file extension of a file so it would be accepted and uploaded to the server, thereby bypassing this simple security model.

Uploading Multiple Files at the Same Time


So far, you have seen some good examples of how to upload a file to the server without much hassle. Now let's take a look at how to upload multiple files to the server from a single page. No built-in capabilities in the Microsoft .NET Framework enable you to upload multiple files from a single ASP.NET page. With a little work, however, you can easily accomplish this task. One trick is to import the S y s t e m . I Oclass into your ASP.NET page, and to then use the
msdn.microsoft.com/en-us/library/aa478971.aspx 10/16

6/20/13

Uploading Files Using the File Field Control

ASP.NET page, and to then use the H t t p F i l e C o l l e c t i o nclass to capture all the files that are sent in with the R e q u e s tobject. This approach enables you to upload as many files as you want from a single page. For this example, you can build an ASP.NET page that has four file-input boxes and one Submit button. After the user clicks the Submit button and the files are posted to the server, the code behind takes the files and saves them to a specific location on the server. After the files are saved, the file information that was posted is displayed in the ASP.NET page (see Listing 3). Listing 3: Uploading multiple files to the server VB < % @I m p o r tN a m e s p a c e = " S y s t e m . I O "% > < % @P a g eL a n g u a g e = " V B "% > < s c r i p tr u n a t = " s e r v e r " > S u bS u b m i t B u t t o n _ C l i c k ( S o u r c eA sO b j e c t ,eA s E v e n t A r g s ) D i mf i l e p a t hA sS t r i n g=" C : \ U p l o a d s " D i mu p l o a d e d F i l e sA sH t t p F i l e C o l l e c t i o n= R e q u e s t . F i l e s D i miA sI n t e g e r=0 D oU n t i li=u p l o a d e d F i l e s . C o u n t D i mu s e r P o s t e d F i l eA sH t t p P o s t e d F i l e= u p l o a d e d F i l e s ( i ) T r y I f( u s e r P o s t e d F i l e . C o n t e n t L e n g t h>0 )T h e n S p a n 1 . I n n e r H t m l+ =" < u > F i l e# "& ( i + 1 )&" < / u > < b r > " S p a n 1 . I n n e r H t m l+ =" F i l eC o n t e n t T y p e :"&_ u s e r P o s t e d F i l e . C o n t e n t T y p e&" < b r > " S p a n 1 . I n n e r H t m l+ =" F i l eS i z e :" &_ u s e r P o s t e d F i l e . C o n t e n t L e n g t h& " k b < b r > " S p a n 1 . I n n e r H t m l+ =" F i l eN a m e :" &_ u s e r P o s t e d F i l e . F i l e N a m e&" < b r > " u s e r P o s t e d F i l e . S a v e A s ( f i l e p a t h& " \ "&_ P a t h . G e t F i l e N a m e ( u s e r P o s t e d F i l e . F i l e N a m e ) )

msdn.microsoft.com/en-us/library/aa478971.aspx

S p a n 1 . I n n e r H t m l+ =" L o c a t i o nw h e r

11/16

6/20/13

Uploading Files Using the File Field Control

S p a n 1 . I n n e r H t m l+ =" L o c a t i o nw h e r es a v e d :"&_ f i l e p a t h&" \ "&_ P a t h . G e t F i l e N a m e ( u s e r P o s t e d F i l e . F i l e N a m e )&_ " < p > " E n dI f C a t c he xA sE x c e p t i o n S p a n 1 . I n n e r H t m l+ =" E r r o r : < b r > "&e x . M e s s a g e E n dT r y i+ =1 L o o p E n dS u b < / s c r i p t > < h t m l > < h e a d > < / h e a d > < b o d y > < f o r me n c t y p e = " m u l t i p a r t / f o r m d a t a "r u n a t = " s e r v e r " > < p > S e l e c tF i l e 1 : < b r/ > < i n p u ti d = " F i l e 1 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < b r/ > S e l e c tF i l e 2 : < b r/ > < i n p u ti d = " F i l e 2 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < b r/ > S e l e c tF i l e 3 : < b r/ > < i n p u ti d = " F i l e 3 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < b r/ > S e l e c tF i l e 4 : < b r/ > < i n p u ti d = " F i l e 4 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < / p > < p > < i n p u ti d = " S u b m i t 1 "t y p e = " s u b m i t "v a l u e = " U p l o a dF i l e s " r u n a t = " S e r v e r "o n s e r v e r c l i c k = " S u b m i t B u t t o n _ C l i c k "/ > < b r/ > < / p > < s p a ni d = " S p a n 1 "r u n a t = " S e r v e r " > < / s p a n > < / f o r m > < / b o d y > < / h t m l > C# < % @I m p o r tN a m e s p a c e = " S y s t e m . I O "% > < % @P a g eL a n g u a g e = " C # "% >
msdn.microsoft.com/en-us/library/aa478971.aspx

< s c r i p tr u n a t = " s e r v e r " >

12/16

6/20/13

Uploading Files Using the File Field Control

< s c r i p tr u n a t = " s e r v e r " > p r o t e c t e dv o i dS u b m i t B u t t o n _ C l i c k ( O b j e c ts e n d e r ,E v e n t A r g se ) { s t r i n gf i l e p a t h=" C : \ \ U p l o a d s " ; H t t p F i l e C o l l e c t i o nu p l o a d e d F i l e s=R e q u e s t . F i l e s ; f o r( i n ti=0 ;i<u p l o a d e d F i l e s . C o u n t ;i + + ) { H t t p P o s t e d F i l eu s e r P o s t e d F i l e=u p l o a d e d F i l e s [ i ] ; t r y { i f( u s e r P o s t e d F i l e . C o n t e n t L e n g t h>0 ) { S p a n 1 . I n n e r H t m l+ =" < u > F i l e# "+ ( i + 1 )+ " < / u > < b r > " ; S p a n 1 . I n n e r H t m l+ =" F i l eC o n t e n t T y p e :"+ u s e r P o s t e d F i l e . C o n t e n t T y p e+" < b r > " ; S p a n 1 . I n n e r H t m l+ =" F i l eS i z e :" + u s e r P o s t e d F i l e . C o n t e n t L e n g t h+ " k b < b r > " ; S p a n 1 . I n n e r H t m l+ =" F i l eN a m e :" + u s e r P o s t e d F i l e . F i l e N a m e+" < b r > " ; u s e r P o s t e d F i l e . S a v e A s ( f i l e p a t h+ " \ \ "+ P a t h . G e t F i l e N a m e ( u s e r P o s t e d F i l e . F i l e N a m e ) ) ; S p a n 1 . I n n e r H t m l+ =" L o c a t i o nw h e r es a v e d :"+ f i l e p a t h+" \ \ "+ P a t h . G e t F i l e N a m e ( u s e r P o s t e d F i l e . F i l e N a m e )+ " < p > " ; } } c a t c h( E x c e p t i o nE x ) { S p a n 1 . I n n e r T e x t+ =" E r r o r :< b r > "+E x . M e s s a g e ; } } } < / s c r i p t >

msdn.microsoft.com/en-us/library/aa478971.aspx

< h t m l >

13/16

6/20/13

Uploading Files Using the File Field Control

< h t m l > < h e a d > < / h e a d > < b o d y > < f o r me n c t y p e = " m u l t i p a r t / f o r m d a t a "r u n a t = " s e r v e r " > < p > S e l e c tF i l e 1 : < b r/ > < i n p u ti d = " F i l e 1 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < b r/ > S e l e c tF i l e 2 : < b r/ > < i n p u ti d = " F i l e 2 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < b r/ > S e l e c tF i l e 3 : < b r/ > < i n p u ti d = " F i l e 3 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < b r/ > S e l e c tF i l e 4 : < b r/ > < i n p u ti d = " F i l e 4 "t y p e = " f i l e "r u n a t = " S e r v e r "/ > < / p > < p > < i n p u ti d = " S u b m i t 1 "t y p e = " s u b m i t "v a l u e = " U p l o a dF i l e s " r u n a t = " S e r v e r "o n s e r v e r c l i c k = " S u b m i t B u t t o n _ C l i c k "/ > < b r/ > < / p > < s p a ni d = " S p a n 1 "r u n a t = " S e r v e r " > < / s p a n > < / f o r m > < / b o d y > < / h t m l > The end user can select up to four files and click the Upload Files button, which initializes the S u b m i t B u t t o n _ C l i c kevent. Using the H t t p F i l e C o l l e c t i o nclass with the R e q u e s t . F i l e s property lets you gain control over all the files that are uploaded from the page. When the files are in this state, you can do whatever you want with them. In this case, the files' properties are examined and written to the screen. In the end, the files are saved to the U p l o a d sfolder in the root directory of the server. The result of this action is illustrated in Figure 5.

msdn.microsoft.com/en-us/library/aa478971.aspx

14/16

6/20/13

Uploading Files Using the File Field Control

Figure 5. Uploading four files at once to the server from a single ASP.NET page As you may have noticed, one interesting point about this example is that the states of the file input text boxes are not saved with the postback. You can see this in Figure 5. In ASP.NET, the state of the file-input text boxes cannot be saved because doing so might pose a security risk.

Conclusion
The File Field control provided by ASP.NET is a powerful control that was quite difficult to achieve in the days of Active Server Pages 3.0. This new capability allows your end-users to upload one or more files to your server. Remember, you can control the size of the files that are uploaded by working with settings in either the m a c h i n e . c o n f i gor w e b . c o n f i gfile.

Related Books
ASP.NET Professional Secrets
msdn.microsoft.com/en-us/library/aa478971.aspx 15/16

6/20/13

Uploading Files Using the File Field Control

Microsoft Corporation. All rights reserved.

2013 Microsoft. All rights reserved.

msdn.microsoft.com/en-us/library/aa478971.aspx

16/16

Das könnte Ihnen auch gefallen