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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <label>请选择一个图像文件</label>
<div>
  <input type="file" id="file" onchange="file_onchange()" />
</div>
<output id="result"> </output>
              
            
!

CSS

              
                
              
            
!

JS

              
                function file_onchange() {
  var file = document.getElementById("file").files[0];
  // 将该文件中前4个字节中的内容复制到一个Blob对象中
  var blob = file.slice(0, 4);
  var reader = new FileReader();
  // 将Blob对象中的数据读取为一个ArrayBuffer对象
  reader.readAsArrayBuffer(blob);
  reader.onload = function(e) {
    var buffer = this.result;
    // 使用DataView读取该ArrayBuffer缓存区中位于开头的32位整数。
    var view = new DataView(buffer);
    var magic = view.getInt32(0, false);
    console.log(magic);
    // 通过比较整数值来判断文件类型
    if (magic < 0) {
      magic = magic + 0x100000000;
      console.log(magic);
      magic = magic.toString(16).toUpperCase();
      console.log(magic);
      if (magic.indexOf("FFD8FF") >= 0) {
        type = "jpg 文件";
      }
      if (magic.indexOf("89504E47") >= 0) {
        type = "png 文件";
      }
      if (magic.indexOf("47494638") >= 0) {
        type = "gif 文件";
      }
      if (magic.indexOf("49492A00") >= 0) {
        type = "tif 文件";
      }
      if (magic.indexOf("424D") >= 0) {
        type = "bmp 文件";
      }
      document.getElementById("result").innerHTML =
        "你选择的文件类型为:" + type;
    }
  };
}

              
            
!
999px

Console