![](/img/trans.png)
[英]JavaScript Download file from Base64 string not working in IE
[英]JavaScript: Extracting base64 string from file reader
我在客戶端(javascript)中有一個Blob圖片,我想將其轉換為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的字符串,怎么做?
在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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.