This is a discussion on Uploading Multiple Files in ASP.NET. within the ASP and ASP.NET Programming forums, part of the Web Development category; Can anybody give me the solution for uploading multiple files in asp.net...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| hi try this code, <%@ Page Language="vb" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>Multiple File Upload</title> </HEAD> <BODY> <ASP:PANEL id="MultipleFileUploadForm" runat="server" visible="true"> <FORM id="Form1" method="post" encType="multipart/form-data" runat="server"> <H1>ASP.NET Multiple File Upload Example</H1> <P>Select the Files to Upload to the Server: <BR> <INPUT id="File1" type="file" name="File1" runat="server"> </P> <P> <INPUT id="File2" type="file" name="File2" runat="server"></P> <P> <INPUT id="Submit1" type="submit" value="Upload Files" name="Submit1" runat="server" OnServerClick="UploadMultipleFiles_Clicked"> </P> </FORM> </ASP:PANEL> <ASP:LABEL id="ResultMsg" runat="server" Visible="False" ForeColor="#ff0033"></ASP:LABEL> </BODY> </HTML> <script runat=”server”> Sub UploadMultipleFiles_Clicked(ByVal Sender As Object, ByVal e As EventArgs) 'Variable to hold the result Dim m_strResultMessage As String 'Variable to hold the FileName Dim m_strFileName As String 'Variable FolderName where the files will be saved Dim m_strFolderName As String = "C:\TempMultipleFiles\" 'Variable to hold the File Dim m_objFile As HttpPostedFile 'Variable used in the Loop Dim i As Integer Try 'Loop Through the Files For i = 0 To Request.Files.Count - 1 'Get the HttpPostedFile m_objFile = Request.Files(i) 'Check that the File exists has a name and is not empty If Not (m_objFile Is Nothing Or m_objFile.FileName = "" Or m_objFile.ContentLength < 1) Then 'Get the name of the file m_strFileName = m_objFile.FileName m_strFileName = Path.GetFileName(m_strFileName) 'Creates the folder if it does not exists If (Not Directory.Exists(m_strFolderName)) Then Directory.CreateDirectory(m_strFolderName) End If 'Save each uploaded file m_objFile.SaveAs(m_strFolderName & m_strFileName) 'Assign the File Name and File Type to Result m_strResultMessage = m_strResultMessage & "Uploaded File: " & m_objFile.FileName & " of type " & m_objFile.ContentType & " <br> " 'Hide the Multiple Form Upload Panel MultipleFileUploadForm.Visible = False End If Next 'If no files where selected provide a user friendly message If m_strResultMessage = "" Then m_strResultMessage = "Select atleast one file to upload." End If Catch errorVariable As Exception 'Trap the exception m_strResultMessage = errorVariable.ToString() End Try 'Unhide the Result Label ResultMsg.Visible = True 'Assign the Result to ResultMsg Label Text ResultMsg.Text = m_strResultMessage End Sub </script> add the form encryption tag, an upload button whose onServerClick calls uploadMultipleFiles_Clicked and the required number of HtmlInputFileControl as the number of files needed to be uploaded for our example it is two. On the server side, the UploadMultipleFiles_Clicked method is where the processing logic resides. First define the location where the files need to be uploaded and finally be saved on the server. The Request object has Files property that defines the instance of HttpFileCollection class, which contains HttpPostedFile objects. Loop through the collection and access each HttpPostedFile object. For each HttpPostedFile object check to see whether the client actually posted the file. If a PostedFile is present get the Full path of the file using the FileName property of HttpPostedFile and than using System.IO.Path just get the file name. Finally invoke the SaveAs method and save the individual files to the desired location. |
| |||
| Any ideas ... Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: The Runat attribute must have the value Server. Source Error: Line 27: </HTML> Line 28: Line 29: <script runat=”server”> Line 30: Sub UploadMultipleFiles_Clicked(ByVal Sender As Object, ByVal e As EventArgs) Line 31: Source File: D:\WWWRoot\nsmaincom\www\content\asp.net\multiplef ileupload\multiplefileupload2.aspx Line: 29
__________________ H2O Without us, no one can survive.. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| file uploading? | saravanan | Java Server Pages (JSP) | 3 | 10-20-2008 12:50 AM |
| uploading and streaming video in asp.net 2.0 application | hareram | ASP and ASP.NET Programming | 2 | 08-26-2007 11:12 PM |
| Page not found error when uploading big size files in ASP .NET | oxygen | ASP and ASP.NET Programming | 1 | 08-09-2007 06:09 AM |
| How to play multiple .flv files using the RTMP connection. | oxygen | Flash Actionscript Programming | 1 | 07-24-2007 05:28 AM |
| Problem in downloading multiple files as an archive. | sureshbabu | PHP Programming | 0 | 07-20-2007 06:16 AM |