Solved

File Upload

Posted on 2011-02-11
6
281 Views
Last Modified: 2012-05-11
Could anyone pls. provide me a working sample of multiple file upload  inside materpage? I am using asp.net 4 and C#.

thanks

ayha
0
Comment
Question by:ayha1999
  • 4
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Anurag Agarwal
ID: 34870311
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

0
 
LVL 7

Author Comment

by:ayha1999
ID: 34870402
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


 
0
 
LVL 8

Accepted Solution

by:
Anurag Agarwal earned 250 total points
ID: 34870464
hmm try this for c# .............
protected void Button1_Click(object sender, EventArgs e)
{
string filepath = "C:\\Uploads";
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
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;
}
}
}

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Author Comment

by:ayha1999
ID: 34870477
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
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34870480
is it for single file or multiple upload?

ayha
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 34870585
thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Graphics 2 30
VB.NET 2008 - SQL Timeout 9 25
VB.NET 2008 Winforms Signing 13 20
I need help with Crystal Report's Parameter Prompt Form 2 15
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question