Avatar of ayha1999
ayha1999

asked on 

File Upload

Could anyone pls. provide me a working sample of multiple file upload  inside materpage? I am using asp.net 4 and C#.

thanks

ayha
ASP.NET.NET ProgrammingC#

Avatar of undefined
Last Comment
ayha1999
Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

hello !!


try this sample code !!

Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim filepath As String = "C:\Uploads"
Dim uploadedFiles As HttpFileCollection = Request.Files
Dim i As Integer = 0
Do Until i = uploadedFiles.Count
Dim userPostedFile As HttpPostedFile = uploadedFiles(i)
Try
If (userPostedFile.ContentLength > 0) Then
Label1.Text += "<u>File #" & (i + 1) & "</u><br>"
Label1.Text += "File Content Type: " & _
userPostedFile.ContentType & "<br>"
Label1.Text += "File Size: " & _
userPostedFile.ContentLength & "kb<br>"
Label1.Text += "File Name: " & _
userPostedFile.FileName & "<br>"
userPostedFile.SaveAs(filepath & "\" & _
System.IO.Path.GetFileName(userPostedFile.FileName))
Label1.Text += "Location where saved: " & _
filepath & "\" & _
System.IO.Path.GetFileName(userPostedFile.FileName) & _
"<p>"
End If
Catch ex As Exception
Label1.Text += "Error:<br>" & ex.Message
End Try
i += 1
Loop
End Sub

Open in new window

Avatar of ayha1999
ayha1999

ASKER

I converted the code to c# then I got an error message

 CS0118: 'uploadedFiles' is a 'variable' but is used like a 'method'

Source Error:
Line 21:             HttpPostedFile userPostedFile = uploadedFiles(i);
 string filepath = "C:\\Uploads";
        HttpFileCollection uploadedFiles = Request.Files;
        int i = 0;
        while (!(i == uploadedFiles.Count))
        {
            HttpPostedFile userPostedFile = uploadedFiles(i);
            try
            {
                if ((userPostedFile.ContentLength > 0))
                {
                    Label1.Text += "<u>File #" + (i + 1) + "</u><br>";
                    Label1.Text += "File Content Type: " + userPostedFile.ContentType + "<br>";
                    Label1.Text += "File Size: " + userPostedFile.ContentLength + "kb<br>";
                    Label1.Text += "File Name: " + userPostedFile.FileName + "<br>";
                    userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));
                    Label1.Text += "Location where saved: " + filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName) + "<p>";
                }
            }
            catch (Exception ex)
            {
                Label1.Text += "Error:<br>" + ex.Message;
            }
            i += 1;
        }

Open in new window


 
ASKER CERTIFIED SOLUTION
Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ayha1999
ayha1999

ASKER

there was some error in the c# code. now it works. where can I filter the type of the uploading file? can u tell how to change the file while uploading?

thanks
Avatar of ayha1999
ayha1999

ASKER

is it for single file or multiple upload?

ayha
Avatar of ayha1999
ayha1999

ASKER

thanks
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo