Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please check this code:- It is not working

protected void Button1_Click(object sender, EventArgs e)
{
string path = Server.MapPath("image");
var filePaths = Directory.GetFiles(path);// this will get all files in given folder path
foreach (var filePath in filePaths)
{
string fileExtension = Path.GetExtension(filePath);
if (FileUpload1.HasFile || fileExtension == ".exe")
{
// you can do ur stuff here

Response.Write("Cannot upload file");
}
else
{
FileUpload1.SaveAs(path + "//" + FileUpload1.FileName);
Response.Write("upload successfully");
}

}
}
Posted

1 solution

"It is not working" is not a good error description - there are so many, many things that could be "not working" as far as you are concerned, and we have no idea what you think the code should do.
However, there are two possibilities which spring to mind:

1) "image" is a file in the same folder as the current webpage. Since you do not specify an absolute path, the relative one will be used. If it's a file, GetFiles will fail.
2) You are assuming that this code is working on the client, rather than the server. C# code cannot access files or folders on the client, only the server, and GetFiles will return a list of files on the server, not the client. So the chances are your FileUpload1 has not loaded any files onto the server at all yet...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900