Replace input type=file by an image

后端 未结 13 2064
感动是毒
感动是毒 2020-12-02 04:13

Like a lot of people, I\'d like to customize the ugly input type=file, and I know that it can\'t be done without some hacks and/or javascript. But,

相关标签:
13条回答
  • 2020-12-02 04:35

    I would use SWFUpload or Uploadify. They need Flash but do everything you want without troubles.

    Any <input type="file"> based workaround that tries to trigger the "open file" dialog by means other than clicking on the actual control could be removed from browsers for security reasons at any time. (I think in the current versions of FF and IE, it is not possible any more to trigger that event programmatically.)

    0 讨论(0)
  • 2020-12-02 04:35

    You can put an image instead, and do it like this:

    HTML:

    <img src="/images/uploadButton.png" id="upfile1" style="cursor:pointer" />
    <input type="file" id="file1"  name="file1" style="display:none" />
    

    JQuery:

    $("#upfile1").click(function () {
        $("#file1").trigger('click');
    });
    

    CAVEAT: In IE9 and IE10 if you trigger the onclick in a file input via javascript the form gets flagged as 'dangerous' and cannot be submmited with javascript, no sure if it can be submitted traditionaly.

    0 讨论(0)
  • 2020-12-02 04:37

    Actually it can be done in pure css and it's pretty easy...

    HTML Code

    <label class="filebutton">
    Browse For File!
    <span><input type="file" id="myfile" name="myfile"></span>
    </label>
    

    CSS Styles

    label.filebutton {
        width:120px;
        height:40px;
        overflow:hidden;
        position:relative;
        background-color:#ccc;
    }
    
    label span input {
        z-index: 999;
        line-height: 0;
        font-size: 50px;
        position: absolute;
        top: -2px;
        left: -700px;
        opacity: 0;
        filter: alpha(opacity = 0);
        -ms-filter: "alpha(opacity=0)";
        cursor: pointer;
        _cursor: hand;
        margin: 0;
        padding:0;
    }
    

    The idea is to position the input absolutely inside your label. set the font size of the input to something large, which will increase the size of the "browse" button. It then takes some trial and error using the negative left / top properties to position the input browse button behind your label.

    When positioning the button, set the alpha to 1. When you've finished set it back to 0 (so you can see what you're doing!)

    Make sure you test across browsers because they'll all render the input button a slightly different size.

    0 讨论(0)
  • 2020-12-02 04:39

    You can replace image automatically with newly selected image.

    <div class="image-upload">
          <label for="file-input">
            <img id="previewImg" src="https://icon-library.net/images/upload-photo-icon/upload-photo-icon-21.jpg" style="width: 100px; height: 100px;" />
          </label>
    
          <input id="file-input" type="file" onchange="previewFile(this);" style="display: none;" />
        </div>
    
    
    
    
    <script>
            function previewFile(input){
                var file = $("input[type=file]").get(0).files[0];
    
                if(file){
                  var reader = new FileReader();
    
                  reader.onload = function(){
                      $("#previewImg").attr("src", reader.result);
                  }
    
                  reader.readAsDataURL(file);
                }
            }
        </script>
    
    0 讨论(0)
  • 2020-12-02 04:41

    This works really well for me:

    .image-upload>input {
      display: none;
    }
    <div class="image-upload">
      <label for="file-input">
        <img src="https://icon-library.net/images/upload-photo-icon/upload-photo-icon-21.jpg"/>
      </label>
    
      <input id="file-input" type="file" />
    </div>

    Basically the for attribute of the label makes it so that clicking the label is the same as clicking the specified input.

    Also, the display property set to none makes it so that the file input isn't rendered at all, hiding it nice and clean.

    Tested in Chrome but according to the web should work on all major browsers. :)

    EDIT: Added JSFiddle here: https://jsfiddle.net/c5s42vdz/

    0 讨论(0)
  • 2020-12-02 04:41

    This is my method if i got your point

    HTML

    <label for="FileInput">
        <img src="tools/img/upload2.png" style="cursor:pointer" onmouseover="this.src='tools/img/upload.png'" onmouseout="this.src='tools/img/upload2.png'" alt="Injaz Msila" style="float:right;margin:7px" />
    </label>
    <form action="upload.php">
        <input type="file" id="FileInput" style="cursor: pointer;  display: none"/>
        <input type="submit" id="Up" style="display: none;" />
    </form>
    

    jQuery

    <script type="text/javascript">
        $( "#FileInput" ).change(function() {
          $( "#Up" ).click();
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题