Solved

File Upload

Posted on 2011-02-11
6
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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