You're here: Snippet Directory » Microsoft .NET » ASP.NET (68)
Language:

Multiple File Upload

Language: English
Programming Language: VB.NET
Published by: Dennis Pallett [not registered]
Last Update: 5/9/2006
Views: 731

Description

Use this codesnippet to upload files to your server. Up to 5 files at one time.

Code

1 <%@ Page Language="vb" %> 2 <script runat="server"> 3 4 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 5 6 If (Me.IsPostBack) Then Me.SaveImages() 7 8 End Sub 9 10 11 12 Private Function SaveImages() As System.Boolean 13 14 'loop through the files uploaded 15 16 Dim colFiles As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files 17 18 19 20 'Message to the user 21 22 Dim strMessage As New System.Text.StringBuilder("Files Uploaded:<br><br>") 23 24 Dim intFileCntr As System.Int32 25 26 Try 27 28 29 30 For intFileCntr = 0 To colFiles.Count - 1 31 32 33 34 Dim objCurrentFile As System.Web.HttpPostedFile = colFiles(intFileCntr) 35 36 Dim strCurrentFileName, strCurrentFileExtension As System.String 37 38 39 40 strCurrentFileName = System.IO.Path.GetFileName(objCurrentFile.FileName) 41 42 43 44 If strCurrentFileName <> doublequote Then 45 46 47 48 strCurrentFileExtension = System.IO.Path.GetExtension(strCurrentFileName) 49 50 51 52 If (strCurrentFileExtension = ".gif") Then 53 54 55 56 'Save File to the proper directory 57 58 objCurrentFile.SaveAs("\\your\site\GIF\" + strCurrentFileName) 59 60 61 62 strMessage.Append(strCurrentFileName + " successfully uploaded.<BR>") 63 64 65 66 ElseIf (strCurrentFileExtension = ".jpg") Then 67 68 69 70 'Save File to the proper directory 71 72 objCurrentFile.SaveAs("\\your\site\JPG\" + strCurrentFileName) 73 74 75 76 strMessage.Append(strCurrentFileName + " successfully uploaded.<BR>") 77 78 79 80 Else 81 82 strMessage.Append(strCurrentFileName &amp; " [colour="red"]failed!! Only .gif and .jpg images allowed![/colour] <BR>") 83 84 End If 85 86 End If 87 88 Next 89 90 91 92 Label1.Text = strMessage.ToString() 93 94 Return True 95 96 Catch Ex As System.Exception 97 98 Label1.Text = Ex.Message 99 100 Return False 101 102 End Try 103 104 105 106 End Function 107 108 109 110 </script> 111 112 <!DOCTYPE HTML Public "-//W3C//DTD HTML 4.0 Transitional//EN" > 113 114 <HTML> 115 116 <HEAD> 117 118 <title>::: UPLOAD SAMPLE ::: </title> 119 120 </HEAD> 121 122 <body> 123 124 <center> 125 126 <form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data"> 127 128 <h3>Multiple File Upload Example</h3> 129 130 <P> 131 132 <INPUT type="file" runat="server" size="50" ID="File1" NAME="File1"></P> 133 134 <P> 135 136 <INPUT type="file" runat="server" size="50" ID="File2" NAME="File2"></P> 137 138 <P> 139 140 <INPUT type="file" runat="server" size="50" ID="File3" NAME="File3"></P> 141 142 <P> 143 144 <INPUT type="file" runat="server" size="50" ID="File4" NAME="File4"></P> 145 146 <P> 147 148 <INPUT type="file" runat="server" size="50" ID="File5" NAME="File5"></P> 149 150 <P><STRONG>:: </STRONG> 151 152 <asp:LinkButton id="LinkButton1" runat="server" Font-Names="Verdana" Font-Bold="True" Font-Size="XX-Small">Upload Images</asp:LinkButton> <STRONG>:: 153 154 </STRONG> [url=java script:document.forms[0].reset()" id="LinkButton2" style="FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana] 155 156 Reset Form[/url] <STRONG>::</STRONG></P> 157 158 <P> 159 160 <asp:Label id="Label1" runat="server" Font-Names="verdana" Font-Bold="True" Font-Size="XX-Small" Width="400px" BorderStyle="None" BorderColor="White"></asp:Label></P> 161 162 <P> </P> 163 164 </form> 165 166 </center> 167 168 </body> 169 170 </HTML>

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS