使用FilePathResult从ASP.Net MVC发送图像文件。当收到http响应时,如何在客户端(Web浏览器)将此图像转换为base64字符串。它在response.data对象中以原始形式显示数据。我试过了
var blob = new Blob([response.data], { type: 'image/jpeg' });
var reader = new FileReader();
reader.onloadend = function () {
var base64data = reader.result;
console.log(base64data);
}
reader.readAsDataURL(blob);
答案 0 :(得分:1)
当您使用ajax将二进制文件作为文本提取时,浏览器将尝试解析字符集并更改您的数据。
您必须将数据作为blob获取,以避免告诉他们不要
function getBase64(blob) {
var blob = xhr.response
var reader = new FileReader();
reader.onload = function () {
var base64data = reader.result;
console.log(base64data);
}
reader.readAsDataURL(blob);
}
var xhr = new XMLHttpRequest()
xhr.open('GET', '/myfile.png', true)
xhr.responseType = 'blob' // get data as blob
xhr.onload = function() {
getBase64(xhr.response)
}
xhr.send()
// or if you are using fetch
fetch('/myfile.png')
.then(function(res) {
res.blob() // get data as blob
.then(getBase64)
})
答案 1 :(得分:0)
我希望我不会被误解:
尝试使用此脚本,以便更轻松地使用jquery:
int bodiesCount = Part.Bodies.count
for (int i = 1; i <= bodiesCount; i++)
{
string name = Part.Bodies.Item(i).Name;
if(name == TextBox.Text("your string value"))
{
Sel.Add(Part.Bodies.Item(i));
MessageBox.Show(i.ToString() + " : " + name);
}
}
您可以根据需要更改上述代码。