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

              
                文件:<input type='file' onchange='openFile(event)'>
<br/>

图片 URL:
<input type='file' accept='image/*' onchange='uploadPicture(event)'>
<div id='picture'>
  
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                // FileReader  
const stateNames = {
    [FileReader.EMPTY] : 'EMPTY',
    [FileReader.LOADING] : 'LOADING',
    [FileReader.DONE] : 'DONE'
  };
let openFile = function(event) {
    let input = event.target;
    let file = input.files[0];
    let reader = new FileReader();
    console.log('file.name: ', file)
    // 打开注解即可查看隐藏属性
    // reader.onloadstart = function() {
    //   reader.abort();
    // };
    reader.onprogress = function(e) {
      console.log('Event: ', e.type)
    };
    reader.onload = function(e) {
      console.log('Event: ', e.type)
    };
    reader.onloadend = function(e) {
      console.log('Event: ', e.type)
      // console.log(reader.error.message);
    };
    reader.onabort = function(e) {
      console.log('Event: ', e.type)
    }
    reader.onerror = function(e) {
      console.log('Event: ', e.type)
      console.log(reader.error.message);
    }
    reader.onload = function(){
      let dataURL = reader.result;
      console.log('ReadyState: ' + stateNames[reader.readyState]);
      console.log('Result: ', dataURL)
    };
    console.log('ReadyState: ' + stateNames[reader.readyState]);
    // 打开注解即可查看隐藏属性
    // reader.readAsDataURL(input.files[0]);
    // reader.readAsArrayBuffer(input.files[0])
    reader.readAsText(file)
    console.log('ReadyState: ' + stateNames[reader.readyState]);
  };

// Blob
let blob = new Blob( new String('hello world'), {type: 'plain/text',endings: 'native'});
let reader_1 = new FileReader();
reader_1.onload = function() {
  let result = reader_1.result;
  console.log('result: ', result, 'type: ', blob.type, 'size: ', blob.size)
}
reader_1.readAsText(blob);

// URL
let picture = document.getElementById('picture');
let uploadPicture = function(event) {
  let target = event.target;
  let file = target.files[0];
  // URL
  let img = document.createElement('img');
  let url = URL.createObjectURL(file);
  img.width = 300;
  img.src = url;
  img.onload = function() {
    URL.revokeObjectURL(url);
  };
  picture.appendChild(img);
  
  // Auto download
  let downLink = document.createElement('a');
  let downUrl = URL.createObjectURL(file, { type: 'image/*' });
  downLink.download = `picture.png`;
  downLink.href = downUrl;
  downLink.click();
  URL.revokeObjectURL(downUrl);

  // FileReader
  let reader_3 = new FileReader();
  reader_3.onload = function() {
    let result = reader_3.result;
    let img = document.createElement('img');
    img.width = 300;
    img.src = result;
    picture.appendChild(img);
  }
  reader_3.readAsDataURL(file);
}
              
            
!
999px

Console