Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <progress value="0" max="100" id="uploader">0%</progress></br>
<input type="file" value="upload" id="uploadBtn"></br></br>
<button id="downloadBtn">download</button></br></br>
<button id="deleteBtn">delete</button></br>
<div id="msg"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                console.clear();
  var firebaseConfig = {
    apiKey: "AIzaSyB3RMejYHAnWxAAk_1R9YtF4EcET7FU8hg",
    authDomain: "fir-a6c96.firebaseapp.com",
    databaseURL: "https://fir-a6c96.firebaseio.com",
    projectId: "fir-a6c96",
    storageBucket: "fir-a6c96.appspot.com",
    messagingSenderId: "918985325909",
    appId: "1:918985325909:web:851298d4d1656730"
  };


const db = firebase.initializeApp(firebaseConfig);

const uploader = document.getElementById("uploader");
const uploadBtn = document.getElementById("uploadBtn");
const downloadBtn = document.getElementById("downloadBtn");
const deleteBtn = document.getElementById("deleteBtn");
const msg = document.getElementById("msg");

let fullPath;
let name;

uploadBtn.addEventListener("change", event => {
  msg.textContent = "";

  // 取得檔案資訊
  const file = event.target.files[0];

  // 路徑包含檔案名稱給.ref指向正確位置
  fullPath = file.name;

  // 檔案名稱在下載時會用到
  name = file.name;

  // 取得 storage 中對應的位置
  const storageReference = firebase.storage().ref(fullPath);

  // .put() 方法把東西丟到該位置裡
  const task = storageReference.put(file);

  // .on()監聽並連動 progress 讀取條
  task.on(
    "state_changed",
    function progress(snapshot) {
      let uploadValue = snapshot.bytesTransferred / snapshot.totalBytes * 100;
      uploader.value = uploadValue;
    },
    function error(err) {
      msg.textContent = "上傳失敗";
    },
    function complete() {
      msg.textContent = "上傳成功";
    }
  );
});

downloadBtn.addEventListener("click", () => {
  // 取得 storage 中對應的檔案位置
  const fileRef = firebase.storage().ref(fullPath);

  // .ref 指向已存在 storage 中的檔案位置後 可以透過 getDownloadURL 取得連結
  fileRef.getDownloadURL().then(function(url) {

    fetch(url)
      .then(res => res.blob())
      .then(blob => {
        let a = document.createElement("a");
      
        // 用 createObjectURL 將 blob 創建一個我們本地端瀏覽器中的 URL 對象
        let url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = name;

        // Firefox 需要將 JS 建立出的 element appendChild 到 DOM 上才可以 work
        a.style.display = "none";
        document.body.appendChild(a);

        a.click();

        // 刪除多餘的 DOM 與 釋放記憶體
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
      });
  });
});

deleteBtn.addEventListener("click",() => {
  // 取得 storage 中對應的檔案位置
  const fileRef = firebase.storage().ref(fullPath);
  
  fileRef.delete().then( () => {
        msg.textContent = "刪除成功";
        uploadBtn.value = "";
        uploader.value = 0;
    }).catch(function (error) {
        msg.textContent = "刪除失敗";
    })
})
              
            
!
999px

Console