JS save base64string as file - JackHu88/Comm GitHub Wiki

Web Service return base64string

[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);

                if (File.Exists(fileLocation))
                    File.Delete(fileLocation);

            }
            catch (Exception ex)
            {
                throw new HttpException(500, ex.Message);
            }
            return base64String;
        }

JS save base64string as file

FileSaver.js https://github.com/eligrey/FileSaver.js

$.ajax({
				    type: "POST",
				    data: {"formData": JSON.stringify(formData)},
				    async: true,
				    url: "/Portal/BizService/GetMethodReturnList",
				    dataType: "json",
				    success: function (data) {
				        var inventData = data.Extend[0].ItemValue;
				        var mimetype=mime.getType(filename);
						var blob=b64toBolb(inventData,mimetype);
						saveAs(blob,filename);
				    }
				})

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;
			}



⚠️ **GitHub.com Fallback** ⚠️