【问题标题】:Preview images before it is uploaded for more than one image [duplicate]在上传多个图像之前预览图像[重复]
【发布时间】:2013-11-06 07:08:15
【问题描述】:

我希望能够在上传之前预览图像,并且我已经自定义了<input type="file"> like

我需要用图片替换带有加号的矩形

我该怎么做?
这是我的 HTML 和 CSS http://jsfiddle.net/YJG79/2/

【问题讨论】:

  • 您需要通过 AJAX 或通过某种 iframe 实现来上传图像。 Uploadify 是一个很棒的,我建议你尝试一下。它使用 jQuery 和 1x1 flash 像素来允许上传而无需实际提交表单。
  • 你可以使用许多插件来做到这一点...做一些网络搜索上传插件
  • @Lix 我检查了 Uploadify 但它没有显示图像,只是进度条:(
  • 是的 - 但它允许您在不重定向用户的情况下上传文件。 Uploadify 将在文件上传后触发,在这种情况下,您可以插入最近上传文件的路径。因此,一旦您捕捉到该事件,您就可以修改您的页面并将src 属性添加到<img> 标记以显示图像。
  • This 可能就是您要搜索的内容。

标签: javascript jquery html css


【解决方案1】:

是的,您可以在上传表单之前显示图像预览

试试这个Example

谢谢。


更新答案:

试试这个JSFiddle example

HTML

<form name="" method="post" action="#" class="feedback-form-1">
    <fieldset>
        <div class="input-file-row-1">
            <div class="upload-file-container">
                <img id="preview_image" src="#" alt="" />
                <div class="upload-file-container-text">
                    <div class = 'one_opacity_0'>
                        <input type="file" id="patient_pic" label = "add" />
                    </div>
                    <span> Add Photo </span>
                </div>
            </div>
        </div>
    </fieldset>
</form>

JQuery:

function readURL(input, target) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        var image_target = $(target);
        reader.onload = function (e) {
            image_target.attr('src', e.target.result).show();
        };
        reader.readAsDataURL(input.files[0]);
     }
 }

 $("#patient_pic").live("change",function(){
    readURL(this, "#preview_image")
 });

CSS:

.input-file-row-1:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}

.input-file-row-1{
    display: inline-block;
    margin-top: 25px;
    position: relative;
}

#preview_image {
  display: none;
  width: 90px;
  height: 90px;
  margin: 2px 0px 0px 5px;
  border-radius: 10px;
}

.upload-file-container { 
    position: relative; 
    width: 100px; 
    height: 137px; 
    overflow: hidden;   
    background: url(http://i.imgur.com/AeUEdJb.png) top center no-repeat;
    float: left;
    margin-left: 23px;
} 

.upload-file-container-text{
    font-family: Arial, sans-serif;
    font-size: 12px;
    color: #719d2b;
    line-height: 17px;
    text-align: center;
    display: block;
    position: absolute; 
    left: 0; 
    bottom: 0; 
    width: 100px; 
    height: 35px;
}

.upload-file-container-text > span{
    border-bottom: 1px solid #719d2b;
    cursor: pointer;
}

.one_opacity_0 {
  opacity: 0;
  height: 0;
  width: 1px;
  float: left;
}

【讨论】:

  • 我试过了,还是不行jsfiddle.net/YJG79/11
  • 我知道您只是想提供帮助,但只是在不注明出处的情况下从另一个(链接的)answer 发布代码并不是我们在Stack Overflow 上的操作方式。您需要正确说明您从哪里获取代码。
  • @Heidel 我在自己的应用程序中使用了相同的代码,而且效果非常好。我会用你的例子来试试,很快就会告诉你。
  • @Lix 是的,我理解您要向我传达的内容,但是我在我的应用程序中应用了同样的东西并且效果很好,所以我只是将其作为参考。我会尽快给出确切的解决方案。谢谢。
  • @Heidel 我已经更新了答案,请检查 jsfiddle 链接,如果您仍然遇到任何问题,请告诉我。谢谢。
猜你喜欢
  • 2019-09-04
  • 2018-08-12
  • 1970-01-01
  • 2011-05-26
相关资源
最近更新 更多