【问题标题】:html file input for setting filePath value用于设置 filePath 值的 html 文件输入
【发布时间】:2015-11-16 06:48:09
【问题描述】:

我有如下的输入文件上传 html 代码。我想设置输入文件的值,然后在输入框中显示图像。但是失败了。你知道为什么吗??有什么解决办法吗??

<div class="mb10">
<input id="image2" name="image2" value="<?php echo $imageFile2;?>" class="file" type="file" accept="image/*">
                                    <div id="uploadImgError2">
                                    </div>
                              </div>

$("#image2").fileinput({
    'showPreview' : true,
    'allowedFileExtensions' : ['jpg', 'png','gif', 'bmp'],
    'showUpload' : false,
    'maxFileCount':1,
    'maxFileSize': 10000000
});

【问题讨论】:

    标签: html file input


    【解决方案1】:

    HTML

    <html>
    <head>
    <link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    <!--[if IE]>
      <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <style>
      article, aside, figure, footer, header, hgroup, 
      menu, nav, section { display: block; }
    </style>
    </head>
    <body>
      <input type='file' onchange="readURL(this);" />
        <img id="image2" src="#" alt="image2" />
    </body>
    </html>
    

    脚本

    function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
    
                reader.onload = function (e) {
                    $('#image2')
                        .attr('src', e.target.result)
                        .width(150)
                        .height(200);
                };
    
                reader.readAsDataURL(input.files[0]);
            }
        }
    

    您可以根据需要更改高度和宽度。

    【讨论】:

    • 其实我还是想用fileinput从网站获取图片并在file input标签中预览。有没有可能???
    猜你喜欢
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多