2022年10月6日 星期四

[Javascript] 將base64轉成Xlsx檔案

Ref: https://juejin.cn/post/7044439580434497549

function b64toBlob(b64Data, contentType = "", sliceSize = 512) {
  const byteCharacters = atob(b64Data);
  const byteArrays = [];

  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize);

    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }

    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }

  const blob = new Blob(byteArrays, { type: contentType });
  return blob;
}

function downloadFromBase64() {
  let b64Data = ""; // xlsx 文件的 base64 編碼
  let contentType = "application/octet-stream";
  const blob = b64toBlob(b64Data, contentType);
  const url = URL.createObjectURL(blob);
  const link = document.createElement("a");
  link.style.display = "none";
  link.href = url;
  link.setAttribute("download", "excel.xlsx");
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}


2022年3月27日 星期日

[URL rewrite] HTTP to HTTPS

在IIS希望網址HTTP重新導向HTTPS
可以使用 URL rewrite的機制



  <rewrite>
       <rules>
           <rule name="HTTP to HTTPS redirect" stopprocessing="true">
               <match url="(.*)">
               <conditions>
                   <add input="{HTTPS}" pattern="off">
               </add></conditions>
               <action redirecttype="Permanent" type="Redirect" url="https://{HTTP_HOST}/{R:1}">
           </action></match></rule>
       </rules>
       <outboundrules>
           <rule enabled="true" name="Add Strict-Transport-Security when HTTPS">
               <match pattern=".*" servervariable="RESPONSE_Strict_Transport_Security">
               <conditions>
                   <add ignorecase="true" input="{HTTPS}" pattern="on">
               </add></conditions>
               <action type="Rewrite" value="max-age=31536000">
           </action></match></rule>
       </outboundrules>
   </rewrite>