html5 <input type=“file” accept=“image/*” capture=“camera”> display as image rather than “choose file” button

前端 未结 4 2132
粉色の甜心
粉色の甜心 2020-12-04 12:14

I\'ve been looking into using html 5 to take a picture from my webapp and upload the image to

4条回答
  •  情话喂你
    2020-12-04 13:04

    You can trigger a file input element by sending it a Javascript click event, e.g.

    
    
    $("#file-input").click();
    

    You could put this in a click event handler for the image, for instance, then hide the file input with CSS. It'll still work even if it's invisible.

    Once you've got that part working, you can set a change event handler on the input element to see when the user puts a file into it. This event handler can create a temporary "blob" URL for the image by using window.URL.createObjectURL, e.g.:

    var file = document.getElementById("file-input").files[0];
    var blob_url = window.URL.createObjectURL(file);
    

    That URL can be set as the src for an image on the page. (It only works on that page, though. Don't try to save it anywhere.)

    Note that not all browsers currently support camera capture. (In fact, most desktop browsers don't.) Make sure your interface still makes sense if the user gets asked to pick a file.

提交回复
热议问题