Search code examples
htmlrazorhtml-helperwebmatrix

Restrict the @fileupload helper to image only


Is there a way to add a line to this to "force" image only?

    @FileUpload.GetHtml(
        initialNumberOfFiles:4,
        allowMoreFilesToBeAdded:false,
        includeFormTag:true,
        ???Image only kind of line????
        uploadText:"Send")

If not, is there a way in the function like

if(IsPost && Validation.IsValid()){
      var uploadedFile1 = Request.Files[0];
      var uploadedFile2 = Request.Files[1];
      if(Path.GetFileName(uploadedFile1.FileName) != String.Empty){
      var fileName1 = Path.GetFileName(uploadedFile1.FileName);
      var fileSavePath1 = Server.MapPath("/path/UploadedFiles/" + DateP + fileName1);
          uploadedFile1.SaveAs(fileSavePath1);
      image1 = "/path/UploadedFiles/" + DateP + fileName1;
      }
      if(Path.GetFileName(uploadedFile2.FileName) != String.Empty){
      var fileName2 = Path.GetFileName(uploadedFile2.FileName);
      var fileSavePath2 = Server.MapPath("/path/UploadedFiles/" + DateP + fileName2);
          uploadedFile2.SaveAs(fileSavePath2);
      image2 = "/path/UploadedFiles/" + DateP + fileName2;  
      }
      ???Like if filename extension is different than jpg or gif, or bmp then "hell no, i'm not uploading this" kind of function???? 

And I know about the accept attribute into the <input type="file" /> but adding it to this code doesn't seems that simple...

Thank you


Solution

  • Finally I've done everything by modifying the code like this

     var filename1b = Path.GetExtension(uploadedFile1.FileName);
     if (filename1b == ".jpg" || filename1b == ".JPG" || filename1b == ".gif" || filename1b == ".GIF" || filename1b == ".bmp" || filename1b == ".BMP" || filename1b == ".png" || filename1b == ".PNG")
    

    It seems to work... I should have took the opportunity to learn more about the modification of the helper, but I think I'm missing to much elements... Sorry Tieson T.