ASPX:
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" /><br />
<asp:Label ID="Label2" runat="server" Text="Invalid File. Please upload a File with Extension .JPEG , .JPG, .PNG" ForeColor="Red" Visible="false"></asp:Label><br /><br />
<asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click" />
<div style="width:50%; float:left; height:400px; overflow:auto;">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Text" HeaderText="Image Name" />
<asp:ImageField DataImageUrlField="Value" HeaderText="Image" ControlStyle-Height="100" ControlStyle-Width="100" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
C#:
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
{
}
}
}
我正在使用ASP.Net和C#。 当我点击上传按钮时,我想检查FileUpload上的每个文件,如果文件扩展名有效(JPEG,JPG,PNG),那么文件保存在文件夹上,如果任何文件不是有效的,那么在Label上显示该文件名,什么也不做。 如何实现这个问题。
答案 0 :(得分:0)
我认为这不是一个在每个文件上传时显示错误的好方法,因为正在上传循环中的文件,因此只有标签上会显示带有任何其他扩展名的最后一个文件,您可以做的是在字符串中添加文件名并且在上传的所有文件的末尾显示以下文件尚未上传的标签,或者您可以在上载具有其他扩展名的文件时在运行时创建标签。
string filenames="";
if(extension="JPG")
uploadfile()
else
filenames+=hpf.FileName+",";
filenames.TrimEnd(",");
label.Text="Following files have not been uploaded "+filenames;
方法2
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.