简体   繁体   English

如何在MVC 4中设置img标签src?

[英]How to set img tag src in MVC 4?

I have a file input control: 我有一个文件输入控件:

<input type="file" id="fileUploadControl" />

On selecting an image file using this file control the selected image src has to be updated in img tag: 使用此文件控件选择图像文件后,必须在img标签中更新所选图像src

<img id="profileImage" width="80%" height="80%" />

I used the folowing jQuery code to update the src : 我使用下面的jQuery代码更新src

$("#fileUploadControl").on('change', function(){
     $("#profileImage ").attr('src', 'url(file://' + $(this).val() + ')');
})

The above code works in ordinary HTML page but when I use this code inside the MVC 4 .cshtml file it didn't work. 上面的代码在普通的HTML页面中有效,但是当我在MVC 4 .cshtml文件中使用此代码时,它不起作用。

What is the reason and how do I overcome this problem? 原因是什么,如何克服这个问题?

I have done this in one of my asp.net project,may this helps you 我已经在我的asp.net项目之一中做到了这一点,可能对您有帮助

FileUpload control FileUpload控件

<asp:FileUpload ID="screenUpload" runat="server" onchange="readURL(this);"/>

Image control 影像控制

<img width="100" id="imgProjImage" runat="server" src=""/>

Function 功能

function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function(e) {
                    $('#imgProjImage').attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM