FileSaver base64 string to file - JackHu88/Comm GitHub Wiki
FileSaver.js
https://github.com/eligrey/FileSaver.js
Blob.js
https://github.com/eligrey/Blob.js
StreamSaver.js
https://github.com/jimmywarting/StreamSaver.js
Mime Type
https://github.com/broofa/node-mime
***** Work for Chrome
function b64toFile(b64Data, filename, contentType) {
var sliceSize = 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var file = new File(byteArrays, filename, {type: contentType});
return file;
}
//var file=b64toFile(base64string,"R8S new.pdf","application/octet-stream");
var file=b64toFile(base64string,"R8S new.pdf","application/pdf");
saveAs(file);
*** Work for Chrome & IE
var blob=b64toBolb(base64string,"application/pdf");
saveAs(blob,"R8S new.pdf");
function b64toBolb(b64Data, contentType){
var sliceSize = 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}
*** C# Web Service
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string DownloadAttrBase64String(string UserPIN, string NoteId, int attPosition)
{
byte[] fileArr;
string base64String = "";
string account = ConfigurationManager.AppSettings["NotesUser"];
SqlAccount.SPCommonFunctionSoapClient sqlAccount = new SqlAccount.SPCommonFunctionSoapClient();
string password = sqlAccount.GetPassword("Sharepoint Online", account);
string _DominoServer = System.Configuration.ConfigurationManager.AppSettings["NotesServer"];
string _DominoUser = System.Configuration.ConfigurationManager.AppSettings["NotesUserFile"];
string _DominoPass = password;
string NotesName = getNotesName(UserPIN);
List<EFormVO> list = new List<EFormVO>();
NotesSessionClass session = null;
NotesDatabase database = null;
NotesDocument document = null;
NotesEmbeddedObject eo = null;
try
{
session = new NotesSessionClass();
session.Initialize(_DominoPass);
database = session.GetDatabase(_DominoServer, _DominoUser, true);
document = database.GetDocumentByUNID(NoteId);
object[] attNameGroup = (object[])document.GetItemValue("AttachmentNames");
string attName = attNameGroup[attPosition].ToString();
eo = document.GetAttachment(attName);
string fileLocation = Server.MapPath("/Attachments/") + attName;
eo.ExtractFile(fileLocation);
BinaryReader binReader = new BinaryReader(File.Open(fileLocation, FileMode.Open, FileAccess.Read));
binReader.BaseStream.Position = 0;
fileArr = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length));
binReader.Close();
base64String = Convert.ToBase64String(fileArr);
}
catch (Exception ex)
{
throw new HttpException(500, ex.Message);
}
return base64String;
}