Solved

File Upload

Posted on 2011-02-11
6
275 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now