【问题标题】:Validate file extension for Upload Multiple File on Folder验证在文件夹上上传多个文件的文件扩展名
【发布时间】:2014-09-11 12:09:01
【问题描述】:

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),然后如果任何文件无效,则将文件保存在文件夹中,然后在标签上显示该文件名并且什么都不做。 如何实现这个问题。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我认为这不是在每个文件上传时显示错误的好方法,因为正在上传循环中的文件,因此标签上只会显示带有任何其他扩展名的最后一个文件你可以做的是在字符串中添加文件名并在所有文件已上传结束时显示以下文件尚未上传的标签,或者您可以在运行时创建标签,只要上传具有其他扩展名的文件。

    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.
    

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 2014-11-06
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多