Showing Base64String as Image works in IE but not

2019-09-19 06:03发布

问题:

C# code: public ActionResult GetDoucmentImage(long onBaseDocumentId) { onBaseDocumentId = 1111; OnBaseDataAccess db = new OnBaseDataAccess(); OnBaseDocument document = db.GetDocument(onBaseDocumentId); return View("GetDoucmentImage", document); }

View Code:

  @{
   var base64 = Convert.ToBase64String(Model.Data);
   var imgSrc = String.Format("data:image/png;base64,{0}", base64);}
   <img src="@imgSrc" style="width:200px; height:400px;"/>

Trying to show an image in browser, it works in IE but not in chrome

回答1:

using chromium, select your image element and right click and select the "Inspect Element" from the context menu....

the chromium Developer tool will open and highlight the client source of the image element....

compare the src value with that shown in the IE Developer tool (f12>Find tab)....

I think MSIE and Gecko browsers sanitise base64 encodings (toStaticHTML), to prevent malicious script injections...

Maybe your Model.Data is not actually a png image.