【问题标题】:Display image after selecting file [closed]选择文件后显示图像[关闭]
【发布时间】:2015-04-05 13:47:32
【问题描述】:

我正在尝试通过 JavaScript 在文件输入中显示所选图像。这是我使用的代码如下所示:

<label id="lblFileUploadProfile">
<asp:FileUpload runat="server" ID="ImageProfileUpload"   />
<asp:Image runat="server" ID="ImgProfilePic" ImageUrl="img/user-5.png"  /> 

我使用此文件输入来上传图片,但不知道选择图片时如何显示所选图片? 我如何在使用 JS 完成上传时显示它?

【问题讨论】:

  • 通过使用 ajax.. 你有什么尝试吗?
  • 从学习如何使用google开始,已经有很多例子了。基本原理如下所示 - 您使用 ajax 启动上传,返回值是两个(或更多) - 如果上传成功以及上传图像的路径。然后只需更新图片的src...
  • 好的,谢谢,但我不知道你为什么这么生气!

标签: javascript c# jquery asp.net image-uploading


【解决方案1】:

试试这个方法:

<img id="blah" class="photo" ImageUrl="img/user-5.png" />
<label for="imgInp" class="custom-file-upload">
    <i class="fa fa-cloud-upload"></i>Add Image
</label>
<input type='file' id="imgInp" name="image" />


<script>
//Preview & Update an image before it is uploaded
    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#imgInp").change(function () {
        readURL(this);
    });
</script>


<style type="text/css">
input[type="file"] {
     display: none;
}

.custom-file-upload {
    border: 1px solid rgb(197, 197, 197);
    border-radius: 4px 4px 4px 4px;

    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
    float: right;
    width: 5.25em;
    margin-left:200px;
}

.photo{
    width: 7em; 
    height: 9em; 
    border: 1px solid rgb(197, 197, 197); 
    border-radius: 4px 4px 4px 4px; 
    float:right;
}
</style>

希望这会有所帮助...

【讨论】:

  • 你确定这对你有用吗?
  • 我在我的 MVC 项目中使用它并且工作得很好。请试试这个,让我知道它是否有效。
  • 我没有使用 MVC,我使用的是 Empty 网站,它对我不起作用它打开选择对话框,但当我选择图像时它什么也不做
  • 使用 Empty 网站或 MVC 没有任何意义。它应该工作。检查姓名和身份证。然后将我的示例集成到您的项目中。它应该可以工作。
  • 另一方面,唯一的问题可能与“ImageUrl”有关。我使用“src”属性而不是这个,但也许这也可以。
猜你喜欢
  • 2012-01-21
  • 2012-09-04
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多