如题,有两种方式
方式一:
标签里直接base64.
方式二:
通过JS发送http请求绑定到标签的src上。
菜鸟教程(runoob.com)
后台方法通过直接读取流:
//HomeController下的方法
public void ProfilePic(string eid)
{
UniversalHumanServiceUtils.GetProfilePic("zhangsan", "zhangsan", HttpContext);
}
Util工具类UniversalHumanServiceUtils下的方法(.NetCore下):
public static void GetProfilePic(string eid, string currUserEid, HttpContext httpContext)
{
var base64String = "/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAABvogAAOPUAAAOQWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAAJKAAAA+EAAC2z3BhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABYWVogAAAAAAAA9tYAAQAAAADTLW1sdWMAAAAAAAAAAQAAAAxlblVTAAAAIAAAABwARwBvAG8AZwBsAGUAIABJAG4AYwAuACAAMgAwADEANv/bAEMAAwICAgICAwICAgMDAwMEBgQEBAQECAYGBQYJCAoKCQgJCQoMDwwKCw4LCQkNEQ0ODxAQERAKD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AiIgIiICIiAiIgIiICIiAp/fZ/Z3Le9Orvg1ZNzyY3WtlpgXdRTVHM4NA9BIyU9Pzj6wBUkOAvInWjW99mc4+HfLTU0wb1254y2YH6NjePqUFiyIiAqnOIeokqdc86klI5m32rjG3o2QtH/QCIg14iIgIiICIiAiIgIiICIiAiIgLbPCjXvt3ELhdRG4gvrJIOgB6SwSRnv7PP/xEQWmoiIP/2Q==";
httpContext.Response.Clear();
if (!string.IsNullOrWhiteSpace(base64String))
{
// Convert base 64 string to byte[]
byte[] bytes = Convert.FromBase64String(base64String);
httpContext.Response.Headers.Clear();
httpContext.Response.Headers.Add("Content-Length", bytes.Length.ToString());
httpContext.Response.ContentType = "image/jpeg";
httpContext.Response.Headers.Add("Content-Disposition", "inline; filename=" + eid);
httpContext.Response.Body.WriteAsync(bytes, 0, bytes.Length);
}
}
.NetCore下Demo效果图:
Util工具类UniversalHumanServiceUtils下的方法(Framework4.8下):
public static void GetProfilePic(string eid, string currUserEid, HttpResponseBase httpResponseBase)
{
var base64String = "/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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ZjJ79NG7YtYRzR02/wCoEPd7cg83BQkWRyG/3TKr9cMkvdSaivudTJVVMp/FI9xcdh5Dr0HkNgscgIiICIiAiIgIiICIiAiIgnjwNa7y5JaH6Q5RWmS42iHxbPLI7rNSN6Oh3Pd0fQt/QdugYpaqnPDMtvGCZXa8wsE5ir7TUsqYTuQHbH4mO27tc3drh5hxCtzw3KrZnGKWnL7O/mo7vSR1cXXctDm7lp92ndp9wUGZREQFED7QvO30WPY3p1STAOuU77nWNHfwovgiB9nPe8/vGFL9Vp8bmRyX7X+60RlL4rHR0lui9APDEzgP/OZ4+iDQqIiAiIgIiICIiAiIgIiICIiAp/fZ/Z3Le9Orvg1ZNzyY3WtlpgXdRTVHM4NA9BIyU9Pzj6wBUkOAvInWjW99mc4+HfLTU0wb1254y2YH6NjePqUFiyIiAqnOIeokqdc86klI5m32rjG3o2QtH/QCIg14iIgIiICIiAiIgIiICIiAiIgLbPCjXvt3ELhdRG4gvrJIOgB6SwSRnv7PP/xEQWmoiIP/2Q==";
httpResponseBase.Clear();
httpResponseBase.ClearContent();
if (!string.IsNullOrWhiteSpace(base64String))
{
// Convert base 64 string to byte[]
byte[] bytes = Convert.FromBase64String(base64String);
httpResponseBase.ClearHeaders();
httpResponseBase.AddHeader("Content-Length", bytes.Length.ToString());
httpResponseBase.ContentType = "image/jpeg";
httpResponseBase.AddHeader("Content-Disposition", "inline; filename=" + eid);
httpResponseBase.Cache.SetCacheability(HttpCacheability.Public);
httpResponseBase.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
httpResponseBase.Cache.SetNoTransforms();
httpResponseBase.Cache.SetETag(eid);
httpResponseBase.Cache.SetMaxAge(new TimeSpan(0, 5, 0));
httpResponseBase.Cache.SetExpires(DateTime.Now.AddMinutes(5));
httpResponseBase.OutputStream.Write(bytes, 0, bytes.Length);
httpResponseBase.Flush();
httpResponseBase.End();
}
}
Framework4.8下Demo效果图:
总结:以上就是两种绑定http返回图片的方法,第一种方式易于理解,第二种方式如果不明白请了解一下HttpResponse。
仅供学习参考,如有侵权联系我删除