IT Community - Software Programming, Web Development and Technical Support

Uploading Multiple Files in ASP.NET.

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...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 08-08-2007, 06:14 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Default Uploading Multiple Files in ASP.NET.

Can anybody give me the solution for uploading multiple files in asp.net
__________________
H2O

Without us, no one can survive..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-08-2007, 06:36 AM
Venkat Venkat is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 350
Venkat is on a distinguished road
Thumbs up Re: Uploading Multiple Files in ASP.NET.

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-08-2007, 06:38 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Thumbs up Re: Uploading Multiple Files in ASP.NET.

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..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-08-2007, 06:39 AM
Venkat Venkat is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 350
Venkat is on a distinguished road
Thumbs up Re: Uploading Multiple Files in ASP.NET.

hi try this,


Hi!
Just correct the line 29.
<script runat="server">.
Should be:
<script language="vb" runat="server">
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 07:33 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0