那位大哥知道怎么把VUE axios向.NET CORE请求的图片显示到Image标签中啊,一直乱码,卡了一周了,网上的方法全都试了一遍,逼得我只有存到本地再取出实现显示图片了,啊啊啊
.NET CORE
[HttpGet]
public IActionResult GetVarifyCode()
{
string varifyCode = string.Empty;
string pathOrigin = new DirectoryInfo(_webHostEnvironment.ContentRootPath).Parent.Parent.Parent.FullName + @"\whww-blog-vue\public\varifyImage";
string time = DateTime.Now.ToString("yyyyMMdd");
string path = pathOrigin + @"\" + time;
if (!Directory.Exists(path))
{
FileHelper.deleteFile(pathOrigin);
Directory.CreateDirectory(path);
}
MemoryStream ms = VarifyCode.Create(out varifyCode, 4);
string file = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpeg";
path = path +@"\"+ file;
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(ms.ToArray());
bw.Close();
}
byte[] vs = Encoding.Default.GetBytes(varifyCode);
HttpContext.Session.Set("varifyCode", vs);
return File(ms.ToArray(), "image/png");
// return File(ms.ToArray(), "application/octet-stream;charset=utf-8");
// return Ok(new AjaxResponse() { Result = @"/varifyImage/"+time+"/"+ file, Success = true });
}
vue
GetVarifyCode() {
this.http
.GetVarifyCode('Public/GetVarifyCode')
.then((res) => {
this.src = res;
console.log(res);
return (
'data:image/png;base64,' +
btoa(
new Uint8Array(res).reduce(
(data, byte) => data + String.fromCharCode(byte),
''
)
)
);
})
.then((data) => {
//this.src = data;
console.log(data);
});