我在客户端有一个blob映像(javascript),我想将它转换为base64字符串。然后,将其传递给后面的代码(C#)。
我使用以下代码将blob转换为base64字符串:
var reader = new FileReader();
reader.onload = function (event) {
createImage(event.target.result); //event.target.results contains the base64 code to create the image.
};
reader.readAsDataURL(blob);//Convert the blob from clipboard to base64
我试图显示读取器对象,以查看我的base64字符串是什么样的。我有这个[object FileReader]
。
我想从它中提取基数64字符串,怎么做呢??
发布于 2018-09-29 06:22:01
在javascript中对base64进行编码/解码的简单方法:
var str ="some sample js to code";
function utoa(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
console.log("CODED: "+utoa(str));
function atou(str) {
return decodeURIComponent(escape(window.atob(str)));
}
console.log("DECODED: "+atou(utoa(str)));
下面也是c#中代码和解码字符串的代码:
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
string encodedString = Base64Encode("sample of text in c#");
Console.WriteLine("CODEE:" + encodedString);
Console.WriteLine("DECODEE:" + Base64Decode(encodedString));
}
public static string Base64Encode(string plainText) {
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData) {
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
}
}
https://stackoverflow.com/questions/52565575
复制相似问题