C#将带有双斜杠的图像数据发送到视图,该双斜杠会破坏图像src

时间:2019-04-04 01:09:49

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

我致力于将从数据库检索到的图像发送到视图。

我将byte[]图像数据转换为字符串,并通过ViewBag将其发送到视图

图像src字符串包含//,该字符串转义了字符串的其余部分,并且在客户端出错。

如何解决这个问题?

错误:

enter image description here enter image description here

控制器:

string imgSrc = null;
string base64 = null;
if (viewModel.Image != null)
{
    base64 = Convert.ToBase64String(viewModel.Image);
    imgSrc = string.Format("data:image/png;base64,{0}", base64);
}
ViewBag.ImageSource  = imgSrc;

查看:

@{
    var propertyId = Model.PropertyId;
    var imageSource = ViewBag.ImageSource;
}
<div id="example" class="k-content">

<div class="demo-section k-content wide">
    <div class="wrapper">
        <div id="images">
            <div class="image">
                <img src="@imageSource"/>
            </div>

        </div>
        <div class="dropZoneElement">
            <div class="textWrapper">
                <p>Add Image</p>
                <p class="dropImageHereText">Drop image here to upload</p>
            </div>
        </div>
    </div>
</div>

<input name="files" id="files" type="file" />

<script type="text/x-kendo-template" id="template">
    <div class="image"></div>
</script>

<script type="text/javascript">
    $(function () {
        var template = kendo.template($("#template").html());

        var initialFiles = [];
        var image = @imageSource;

        $("#images").html(kendo.render(template, initialFiles));

        $("#images").append('<div class="image"><img src="' + image + '" /></div>');

        $("#files").kendoUpload({
            async: {
                saveUrl: "save?id=" + @propertyId,
                removeUrl: "remove",
                autoUpload: true
            },
            multiple: false,
            validation: {
                allowedExtensions: [".jpg", ".jpeg", ".png", ".bmp", ".gif"]
            },
            success: onSuccess,
            dropZone: ".dropZoneElement"
        });

        function onSuccess(e) {
            if (e.operation == "upload") {
                var file = e.files[0].rawFile;

                if (file) {
                    var reader = new FileReader();

                    reader.onloadend = function () {
                        $("#images").empty().append("<div class='image'><img src=" + this.result + " /></div>");
                    };

                    reader.readAsDataURL(file);
                }
            }
            if (e.operation == "remove") {
                $("#images").empty();
            }
        }
    });

</script>

1 个答案:

答案 0 :(得分:1)

此行引起了问题:

'<div class="image"><img src=' + @imageSource + ' /></div>'

还有这一行:

var image = @imageSource;

应为:

var image = "@imageSource";

将其更改为:

'<div class="image"><img src="' + image + '" /></div>'
// Add double quotes         ^-------------^ 

您要在图像src中添加不带双引号的字符串image,因此它类似于:

<img src=some string /> 

但是应该像这样:

<img src="some string" /> 

然后将@imageSource更改为image

您当然可以使用单引号,但需要像这样对它们进行换行:

'<div class="image"><img src=\'' + image + '\' /></div>'

解决方案2

只需删除行:

var image = @imageSource;

从您的JavaScript中更改行:

$("#images").append('<div class="image"><img src="' + image + '" /></div>');

收件人:

$("#images").append('<div class="image"><img src="@imageSource" /></div>');