2014-09-11 2 views
0

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 번호 : 내가 ASP.Net과 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 
      { 
      } 
     } 
    } 

. 업로드 버튼을 클릭하면 File Extension이 Valid (JPEG, JPG, PNG) 인 경우 FileUpload의 모든 파일을 확인한 다음 파일이 유효하지 않으면 폴더에 파일을 저장하고 Label에 해당 파일 이름을 표시하고 아무 것도하지 않습니다. 이 문제를 해결하는 방법.

답변

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.