DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • BFC 块级格式化上下文
  • Note
  • WebAssembly
  • public api
  • 位运算
  • bitwise operator
  • css实现隐藏效果
  • css snippets
  • 抖音点赞
  • js 相等判断
  • fetch ReadableStream
  • git
  • Github Actions 工作流
  • google search
  • RPC vs HTTP
  • gravatar
  • hhkb
  • Init project
  • input 文件上传
  • mac

    • Mac 使用技巧
    • alfred
    • mac shortcuts
    • shortcuts text edit
    • mac 修改host
  • 微前端
  • mock
  • nginx dump
  • nginx
  • NirCmd
  • npm
  • Operator Precedence
  • package.json
  • url query 解析
  • pnpm
  • JavaScript Precise countdown
  • react 模版
  • regexp
  • setup web development
  • telegram

    • telegram bot
  • timeFunction ease
  • 视频裁剪
  • vscode

    • vscode 高级指南
    • bracketPairs
    • jsconfig.json
    • vscode pipe into code
    • social project
    • vscode tasks
  • draggable resizable
  • windows 激活
  • 前端截图实现
  • 文本配音 富文本实现
  • 图片处理
  • 前端坐标
  • 定时任务
  • work efficient
  • 微信小程序动画实现方案
  • 排列组合
  • 数列
  • 语音驱动文字
  • 浏览器
  • 状态管理
  • 移动盒子
  • 移动端开发常用snippets
  • 设计模式
  • web performance

input 文件上传

图片上传

1. 主要涉及内容

  • input accept
  • filesList
  • URL.createObjectURL()
  • URL.revokeObjectURL()

2. input file

<label for='upload'></label> //  ::before :: after  用于扩展可点击区域
<input type="file" id="upload" accept="image/*" onchange="upload(this.files)" >  // 'video/*' 'audio/*'  'image/*'

获取 filsList对象

const selectedFile = document.getElementById('upload').files[0];

onchange="upload(this.files)"  

const inputElement = document.getElementById("upload");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files;  // Filelist 对象
}

3. 自定义上传框

  • 隐藏input 框 使用 click 方法
<input type="file" id="upload"  accept="image/*" style="display:none">
<button id="uploadBox">click to select files </button>
const inup = documnet.querySelector('#upload');
const inputBox = documnet.querySelector('#uploadBox');

inputBox.addEventListener("click", function (e) {
  if (input) {
    input.click();
  }
}, false);
  • label
<input type="file" id="upload"  accept="image/*" class="visually-hidden">
<button id="uploadBox">click to select files </button>
// 不能使用 hidden display:none; 隐藏元素
// 使用定位  clicp  || opcacity:0  ...

.visually-hidden {
  position: absolute !important;
  height: 1px;
  width: 1px;
  overflow: hidden;
  clip: rect(1px, 1px, 1px, 1px);
}
  • ::before ::after
// 不能使用 opacity hidden display:none 
// 使用定位 + overfollow:hidden
.upload::before {  
 content: '';
 position: absolute;
 top: 0;
 left: 0;
 bottom: 0;
 right: 0;
 background:whitesmoke;
 width: 100%;
 height: 100%;
}
  1. 使用URL 对象实现本地预览
const url = window.URL.createObjectURL(files[i])
let img = documnet.createElement('img');
img.src = url;
img.height = 100;
img.onload = function() {
  window.URL.revokeObjectURL(this.src);
}

视频上传

获取 fileList 对象的方式、预览 跟上传图片一样 这里主要介绍一下获取视频 duration

<label for='upload'></label> //  ::before :: after  用于扩展可点击区域
<input type="file" id="upload" accept="video/*" onchange="upload(this.files)" >  // 'video/*' 'audio/*'  'image/*'

const input = document.querySelector('#upload');
function handleInput() {
const { files } = this;

const video = document.createElement('video');

// preload = 'metadata'
video.preload = 'metadata';
video.src = window.URL.createObjectURL(files[0]);


function loadMetaData(params) {
  // 要实现本地预览则可以不用 revoke
  window.URL.revokeObjectURL(video.src);

  const { duration } = video;

  console.log('😄 video duration ', duration);
}
// 监听 loadmetadata 事件
video.addEventListener('loadedmetadata', loadMetaData, false);

input.append(video);
}
input.addEventListener('change', handleInput, false);

Refer:

  • mdn: createObjectURL
  • mdn: input:file
  • mdn: using files from web application
Last Updated:
Contributors: rosendo
Prev
Init project