npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-preview.js

v1.0.2

Published

簡單、快速取得使用者選取的多個圖檔資訊,適合用在預覽圖、檔案 Base64 編碼。

Downloads

1

Readme

file-preview.js

簡單、快速取得使用者選取的多個圖檔資訊,適合用在預覽圖、檔案 Base64 編碼。

透過 NPM 安裝

npm i file-preview.js

簡單用法

在你的 HTML 中添加 jQuery, file-preview.js

<script src="jquery-3.1.1.min.js"></script>
<script src="dist/jquery.file-preview.js"></script>

接著通常有類似這樣的結構

<form class="form1">
    <input type="file" name="upload[]" class="upload" multiple>    
</form>

如果只是顯示預覽圖片,jQuery 的部分可以這麼寫

$().filePreview({
    parent: ".form1",
    selector: ".upload",
    success: function (key, obj){
        console.log(obj)
    }
})

可以透過 FileReader() 取得圖片或檔案的 Base64 編碼,例如您打算使用 Base64 為圖片編碼後上傳到伺服器,可以這麼用

$().filePreview({
    parent: ".form1",
    selector: ".upload",
    isReader: true,
    progress: function (key, percent){
        console.log(percent)
    },
    success: function (key, obj){
        console.log(obj)
    }
})

在 success 的時候可以在 obj.base64 取得編碼。但注意,這會犧牲瀏覽器的記憶體效能,檔案過大則會容易造成記憶體不足。通常這用在上傳圖檔時很適合。

API

$.filePreview.create(selector, param)

可以透過自訂的事件配合,例如

$(".form1").on('change', '.upload', function() {
    var _this = this;
    $.filePreview.create(this, {
        isReader: false,
        progress: function (key, percent){
            console.log(percent)
        },
        success: function (key, obj){
            var $img = $(_this).parents(".form1").find(".img").eq(key);
            $img.css({
                'background-image' : 'url(' + obj.preview + ')'
            });
        }
    });
});

$.filePreview.version()

取得版本

參數

  • parent 綁定的父元素
  • selector 綁定的元素
  • isReader 是否啟用 FileReader
  • progress(key, percent) 若啟用 FileReader 可顯示讀取進度
  • success(key, obj) 單一檔案讀取成功所觸發

progress 回傳

  • percent 讀取進度百分比

success 回傳

  • file 檔案原始資訊
  • nameExtension 附檔名
  • nameMaster 主檔名
  • preview 預覽網址
  • size 檔案大小
  • type 檔案類型
  • base64 檔案使用 Base64 編碼