javascript - How to set JSON result to 'src' of 'img' to show an image in ASP.NET MVC4? -
i’ve got absolute path of image in json format ajax @ view. looks like:
var addresimg=data.result.sourcefile;//address 'd:/jqueryfileuploadmvc4/mvc4appl/app_data/1.png'
however, view cannot render image attribute “src” image should like:
<img src="@url.content("~/app_data /" + "1.png")" />
but src of view is: // not correct address of image should address server
js:
<script type="text/javascript"> $(document).ready(function () { $('#fileupload').fileupload({ datatype: 'json', url: '/home/uploadfiles', autoupload: true, done: function (e, data) { var addresimg=data.result.sourcefile;//address 'd:/jqueryfileuploadmvc4/mvc4appl/app_data/1.png'' $(".file_source").attr('src', data.result.sourcefile);//it sets absolute path , incorrect. image iddress should src="@url.content("~/app_data /" + "1.png")" } }).on('fileuploadprogressall', function (e, data) { var progress = parseint(data.loaded / data.total * 100, 10); $('.progress .progress-bar').css('width', progress + '%'); }); }); </script>
is possible assign 'src' attribute result of json?
the json data should include relative or absolute url, not filesystem path. browser doesn't have access server's filesystem directly, src attribute has use url.
the server has generate correct url mapping filesystem path so:
var imageurl = url.content("~/app_data/1.png");
the server-side logic populates model have set sourcefile property , serialized , sent client json. example
var model = getmodelfromdatabase(); //any code sets model object model.sourcefile = imageurl;
you able use var addresimg=data.result.sourcefile;
without problem
Comments
Post a Comment