C# 验证文件夹上上载多个文件的文件扩展名
ASPX:C# 验证文件夹上上载多个文件的文件扩展名,c#,asp.net,C#,Asp.net,ASPX: C#: 受保护的无效按钮1\u单击(对象发送者,事件参数e) { DateTime curr=DateTime.Now; DateTime INDIAN_ZONE=TimeZoneInfo.ConvertTimeBySystemTimeZoneId(当前,“印度标准时间”); if(FileUpload1.HasFile) { HttpFileCollection hfc=Request.Files; 对于(int i=0;i0) { 字符串fileextension=Syst
C#:
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
DateTime curr=DateTime.Now;
DateTime INDIAN_ZONE=TimeZoneInfo.ConvertTimeBySystemTimeZoneId(当前,“印度标准时间”);
if(FileUpload1.HasFile)
{
HttpFileCollection hfc=Request.Files;
对于(int i=0;i0)
{
字符串fileextension=System.IO.Path.GetExtension(FileUpload1.FileName);
如果(文件扩展=“.jpg”)
{
字符串time1=印度地区。ToString(“MM-dd-yyyy\U hhmmss”);
string directoryPath=Server.MapPath(string.Format(“./upload/”+TextBox1.Text));
如果(!Directory.Exists(directoryPath))
{
CreateDirectory(directoryPath);
}
其他的
{
}
字符串文件名=Path.GetFileName(hpf.fileName);
fileName=time1+fileName;
字符串路径=“./upload/”+TextBox1.Text+“/”;
SaveAs(Server.MapPath(path)+文件名);
}
其他的
{
}
}
}
string[]filepath=Directory.GetFiles(Server.MapPath(“~/upload/”+TextBox1.Text+“/”);
列表文件=新列表();
foreach(文件路径中的字符串文件路径)
{
字符串fileName1=Path.GetFileName(filePath);
添加(新列表项(fileName1,~/upload/“+TextBox1.Text+”/“+fileName1));
}
GridView1.DataSource=文件;
GridView1.DataBind();
}
其他的
{
字符串time1=印度地区。ToString(“MM-dd-yyyy\U hhmmss”);
string directoryPath=Server.MapPath(string.Format(“./upload/”+TextBox1.Text));
如果(!Directory.Exists(directoryPath))
{
CreateDirectory(directoryPath);
}
其他的
{
}
}
}
我正在使用ASP.Net和C#。
当我点击上传按钮时,我想检查FileUpload上的每个文件是否文件扩展名有效(JPEG、JPG、PNG),然后将文件保存到文件夹中,如果任何文件无效,则在标签上显示该文件名,并且什么也不做。
如何解决这个问题。我认为这不是一个显示每个文件上载错误的好方法,因为循环中的文件正在上载,所以标签上只显示最后一个具有任何其他扩展名的文件。您可以做的是在字符串中添加文件名,并在所有已上载文件的末尾显示标签,说明以下文件尚未上载上载或您可以在运行时创建标签,无论何时上载具有其他扩展名的文件
protected void Button1_Click(object sender, EventArgs e)
{
DateTime curr = DateTime.Now;
DateTime INDIAN_ZONE = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(curr, "India Standard Time");
if (FileUpload1.HasFile)
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
string FileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
if (FileExtention == ".jpg")
{
string time1 = INDIAN_ZONE.ToString("MM-dd-yyyy_hhmmss");
string directoryPath = Server.MapPath(string.Format("./upload/" + TextBox1.Text));
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
else
{
}
string fileName = Path.GetFileName(hpf.FileName);
fileName = time1 + fileName;
string path = "./upload/" + TextBox1.Text + "/";
hpf.SaveAs(Server.MapPath(path) + fileName);
}
else
{
}
}
}
string[] filePaths = Directory.GetFiles(Server.MapPath("~/upload/" + TextBox1.Text + "/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
string fileName1 = Path.GetFileName(filePath);
files.Add(new ListItem(fileName1, "~/upload/" + TextBox1.Text + "/" + fileName1));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
else
{
string time1 = INDIAN_ZONE.ToString("MM-dd-yyyy_hhmmss");
string directoryPath = Server.MapPath(string.Format("./upload/" + TextBox1.Text));
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
else
{
}
}
}
方法2
string filenames="";
if(extension="JPG")
uploadfile()
else
filenames+=hpf.FileName+",";
filenames.TrimEnd(",");
label.Text="Following files have not been uploaded "+filenames;
string filenames="";
if(extension="JPG")
uploadfile()
else
filenames+=hpf.FileName+",";
filenames.TrimEnd(",");
label.Text="Following files have not been uploaded "+filenames;
if(extension="JPG")
uploadfile()
else
Label lb=new Label();
lbl.Text=hpf.FileName+" not uploaded please upload with JPG";
//Add lbl to your div or any other control.