webuploader 파일 업로드 컴포넌트 개발 예
최근 프로젝트에서는 대용량 파일을 분할해서 업로드하고 백엔드 fastdfs에 연결하기 위해 Baidu의 webuploader를 사용해야 했기 때문에 파일 업로드를 위한 이 작은 플러그인을 작성하기 시작했습니다. 단계는 매우 간단하지만, 또한 많은 것이 있습니다. 제가 추측할 수 있는 함정은 제가 개발한 vue webuploader의 파일 업로드 구성요소를 주로 소개하는 것입니다. 이제 여러분과 공유하고 참고할 수 있기를 바랍니다.
자세한 내용은 다음과 같습니다.
1. Encapsulation 컴포넌트
Baidu에서 제공하는 webuploader.js와 Uploader.swf를 소개합니다.
css 스타일을 컴포넌트에 직접 작성합니다.
<template> <p> <p id="list" class="uploader-list"></p> <p id="wrapper"> <p class="uploader-container"> <p :id="id" limitSize="1" :ext="ext"></p> <el-button style="margin-bottom:10px;float:left;" size="small" :loading="uploadLoading" type="success" @click="start">上传到服务器</el-button> <el-button style="margin-left: 20px;margin-bottom:10px;float:left;" :disabled="stopBtn" size="small" type="danger" @click="stop">暂停上传</el-button> </p> </p> <p class="el-upload__tip">{{tip}}</p> <p class="file-list"> <ul class="el-upload-list el-upload-list--text"> <li v-for="file in fileList" :class="['el-upload-list__item', 'is-' + file.status]" :key="file"> <a class="el-upload-list__item-name"> <i class="el-icon-document"></i>{{file.name}} </a> <label class="el-upload-list__item-status-label"> <i :class="{'el-icon-upload-success': true,'el-icon-circle-check': listType === 'text', 'el-icon-check': ['picture-card', 'picture'].indexOf(listType) > -1}"></i> </label> <i class="el-icon-close" @click="removeFile(file)"></i> <el-progress v-if="file.status === 'uploading'" :type="listType === 'picture-card' ? 'circle' : 'line'" :stroke-width="listType === 'picture-card' ? 6 : 2" :percentage="file.percentage"> </el-progress> </li> </ul> </p> </p> </template> <script> import '../js/jquery.js' import '../js/webuploader.js' import { Base64 } from 'js-base64' import CryptoJS from 'crypto-js'; export default{ name: 'fileUpload', props: { id: { type: String, default: function(){ return "filePicker"; } }, //上传提示 tip: { type: String, default: function(){ return ""; } }, //文件后缀名限制 ext: { type: String, default: function(){ return "jpg,jpeg,png,pdf,mp4,avi.mp3"; } }, //分片大小设置 chunkSize: { type: Number, default: function(){ return 2097152; } }, //分片上传重试次数 chunkRetry: { type: Number, default: function(){ return 1; } }, //是否自动上传 auto: { type: Boolean, default: function(){ return false; } }, //上传文件大小限制 sizeLimit: { type: Number, default: function(){ return 209715200; } }, //上传文件数量限制 countLimit: { type: Number, default: function(){ return 5; } } }, data(){ return{ appId: AppConfig.appId, securityKey: AppConfig.securityKey, checkUrl: AppConfig.checkUrl, uploadUrl: AppConfig.uploadUrl, mergeUrl: AppConfig.mergeUrl, previewName: '选择文件', wul_fileMd5: '', wul_size: 0, wul_fileName: '', wul_chunk: 0, wul_uploader: '', fileList: [], listType: 'text', percentage: 0, fileObject: { uid: '', name: '', ext: '', type: '', status: '', percentage: 0, url: '' }, uploadLoading: false, stopBtn: true } }, methods: { /** * 获取当前上传列表中的文件 * @returns {Array|*} */ getFileList: function(){ return this.fileList; }, //绑定事件 wul_init: function() { //提示只能选择一个文件 this.wul_uploader.on('filesQueued', function (files) { if (files.length > 1) { this.$message({ message: '请选择一张图片', type: 'error' }); for (var i = 0; i < files.length; i++) { this.wul_uploader.cancelFile(files[i]); } this.wul_uploader.reset(); this.wul_fileMd5 = ""; this.wul_size = 0; this.wul_fileName = ""; this.wul_chunk = 0; //当前切片数 }else{ if( this.fileList.length == this.countLimit ){ this.$message({ message: '已经达到上传文件限制数量', type: 'error' }); }else{ //此时往需要上传的文件列表中添加文件 let file = { uid: Date.now() + this.tempIndex++, name: files[0].name, type: files[0].type, ext: files[0].ext, status: "ready", percentage: 0 } this.fileObject = file; this.fileList.push(this.fileObject); } } }.bind(this)); //文件校验格式和大小 this.wul_uploader.on('error', function (type) { debugger if (type == 'Q_EXCEED_SIZE_LIMIT') { this.$message({ message: '文件超过指定大小', type: 'error' }); } if (type == 'Q_TYPE_DENIED') { this.$message({ message: '文件格式错误,请选择文件', type: 'error' }); } if (type == 'F_EXCEED_SIZE') { this.$message({ message: "文件超过" + this.sizeLimit / 1024 / 1024 + "M", type: 'error' }); } }.bind(this)); //上传进度 this.wul_uploader.on('uploadProgress', function (file, percentage) { this.percentage = percentage * 100; this.fileObject.status = "uploading"; this.fileObject.percentage = this.percentage; console.log(this.fileObject.percentage); }.bind(this)); //每次切片上传完成之后的判断 this.wul_uploader.on('uploadAccept', function (object, ret) { if (ret.responseCode != 0) { this.wul_uploader.cancelFile(this.wul_uploader.getFiles()[0].id); } }); this.wul_uploader.on('uploadBeforeSend', function(object, data, headers) { console.log(data); }); }, option: function(key, val) { this.wul_uploader.option(key, val); var options = this.wul_uploader.options; this.wul_uploader.destroy(); //注销uploader this.wul_uploader = WebUploader.create(options); this.wul_init(); }, start: function(){ if(this.wul_uploader.getFiles()[0] != null) { this.wul_uploader.upload(this.wul_uploader.getFiles()[0].id); this.uploadLoading = true; this.stopBtn = false; } else { this.$message({ message: "请选择上传文件", type: 'error' }); } }, stop: function(){ this.wul_uploader.cancelFile(this.wul_uploader.getFiles()[0].id); }, removeFile: function(file){ this.fileList.splice(this.fileList.indexOf(file), 1); }, change: function(){ this.option('accept', { title: 'Images', extensions: 'gif,jpg,jpeg,bmp,png' }); } }, mounted(){ WebUploader.Uploader.register({ "before-send-file": "beforeSendFile", "before-send": "beforeSend", "after-send-file": "afterSendFile", }, { beforeSendFile: function (file) { var deferred = WebUploader.Deferred(); this.wul_uploader.md5File(file).then(function (val) { this.wul_fileMd5 = val; this.wul_size = file.size; this.wul_fileName = file.name; var timestamp = Date.parse(new Date()) / 1000; var signParam = "{chunkSize=" + this.chunkSize + ", fileMd5=" + this.wul_fileMd5 + ", size=" + this.wul_size + ", timestamp=" + timestamp + "}"; var sign = Base64.encode(CryptoJS.HmacSHA1(signParam, this.securityKey)); // 获取断点续传位置 jQuery.ajax({ type: "POST", // 测试 url: this.checkUrl, data: { // 文件大小 size: this.wul_size, // 文件唯一标记 fileMd5: this.wul_fileMd5, // 切片大小 chunkSize: this.chunkSize, // 签名 sign: sign, // 应用分配id appId: this.appId, // 当前时间戳 timestamp: timestamp }, dataType: "json", // 上传失败 error: function (XMLHttpRequest, textStatus, errorThrown) { this.$message({ message: "上传失败...", type: 'error' }); this.uploadLoading = false; this.stopBtn = true; }.bind(this), success: function (response) { if (response.responseCode == 0) { // 切片获取成功 this.wul_chunk = response.chunk; deferred.resolve(); } else { // 切片获取失败,请求成功 this.wul_uploader.cancelFile(file); //取消文件上传 this.$message({ message: "切片检查失败,请联系管理员", type: 'error' }); deferred.resolve(); this.uploadLoading = false; this.stopBtn = true; } }.bind(this) }); return deferred.promise(); }.bind(this)); return deferred.promise(); }.bind(this), beforeSend: function (block) { var deferred = WebUploader.Deferred(); if (block.chunk < this.wul_chunk) { return deferred.reject(); } this.wul_uploader.md5File(block.blob).then(function (chunkMd5) { var timestamp = Date.parse(new Date()) / 1000; var signParam = '{chunk=' + block.chunk + ', chunkMd5=' + chunkMd5 + ', chunkSize=' + this.chunkSize + ', fileMd5=' + this.wul_fileMd5 + ', size=' + this.wul_size + ', timestamp=' + timestamp + '}'; var signTemp = CryptoJS.HmacSHA1(signParam, this.securityKey); var sign = Base64.encode(signTemp); //获取sign值 this.wul_uploader.options.formData = { 'timestamp': timestamp, 'appId': this.appId, 'chunk': block.chunk, 'chunkSize': this.chunkSize, 'fileMd5': this.wul_fileMd5, 'chunkMd5': chunkMd5, 'size': this.wul_size, 'sign': sign }; deferred.resolve(); }.bind(this)) return deferred.promise(); }.bind(this), afterSendFile: function (file) { var timestamp = Date.parse(new Date()) / 1000; var signParam = "{chunkSize=" + this.chunkSize + ", fileMd5=" + this.wul_fileMd5 + ", fileName=" + file.name + ", size=" + this.wul_size + ", timestamp=" + timestamp + "}"; var sign = Base64.encode(CryptoJS.HmacSHA1(signParam, this.securityKey)); // 如果分块上传成功,则通知后台合并分块 jQuery.ajax({ type: "POST", url: this.mergeUrl, data: { appId: this.appId, fileMd5: this.wul_fileMd5, fileName: file.name, chunkSize: this.chunkSize, sign: sign, size: this.wul_size, timestamp: timestamp }, success: function (response) { if (response.responseCode == 0) { this.fileObject.status = "success"; this.fileObject.percentage = 100; this.fileObject.url = response.filePath; } else { this.fileObject.status = "exception"; this.$message({ message: "上传失败,失败原因:" + response.responseMsg, type: 'error' }); } this.uploadLoading = false; this.stopBtn = true; this.wul_uploader.reset(); this.wul_fileMd5 = ""; this.wul_size = 0; this.wul_fileName = ""; this.wul_chunk = 0; //当前切片数 }.bind(this) }); }.bind(this) }); this.wul_uploader = WebUploader.create({ // swf文件路径 swf: '../js/Uploader.swf', // 文件接收服务端。 server: this.uploadUrl, // 定义选择按钮 pick: { "id": "#" + this.id, "innerHTML": this.previewName }, // 自动上传 auto: this.auto, // 禁止浏览器打开文件 disableGlobalDnd: true, // 添加截图功能 paste: '#wrapper', // 定义拖动面板 dnd: '#wrapper', // 分片上传 chunked: true, // 分片大小为2M chunkSize: this.chunkSize, // 分片上传失败重试次数 chunkRetry: this.chunkRetry, // 图片不做压缩 compress: false, // 队列设置10个,为了选择多个文件的时候能提示 fileNumLimit: 10, // 提前准备好下一个文件 prepareNextFile: true, // 限制单个文件大小 fileSingleSizeLimit: this.sizeLimit, //线程数 threads : 1, // 限制格式 accept: { title: "access", extensions: this.ext } }); this.wul_init(); } } </script> <style> /* ----------------Reset Css--------------------- */ html, body, p, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, figcaption, figure, footer, header, hgroup, menu, nav, section, summary, time, mark, audio, video, input { margin: 0; padding: 0; border: none; outline: 0; font-size: 100%; font: inherit; vertical-align: baseline; } html, body, form, fieldset, p, p, h1, h2, h3, h4, h5, h6 { -webkit-text-size-adjust: none; } article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { font-family: arial, sans-serif; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } ins { text-decoration: none; } del { text-decoration: line-through; } table { border-collapse: collapse; border-spacing: 0; } /* ------------ */ #wrapper { width: 100%; margin: 0 auto; height: 35px; } .img-preview { width: 160px; height: 90px; margin-top: 1em; border: 1px solid #ccc; } .cropper-wraper { position: relative; } .upload-btn { background: #ffffff; border: 1px solid #cfcfcf; color: #565656; padding: 10px 18px; display: inline-block; border-radius: 3px; margin-left: 10px; cursor: pointer; font-size: 14px; position: absolute; right: 1em; bottom: 2em; } .upload-btn:hover { background: #f0f0f0; } .uploader-container{ width: 100%; font-size: 10px; } .webuploader-container { position: relative; width: 100px; height: 21px; float: left; } .webuploader-element-invisible { position: absolute !important; clip: rect(1px 1px 1px 1px); /* IE6, IE7 */ clip: rect(1px,1px,1px,1px); } .webuploader-pick { position: relative; display: inline-block; cursor: pointer; background: #00b7ee; padding: 6px 15px; color: #fff; text-align: center; border-radius: 3px; overflow: hidden; } .webuploader-pick-hover { background: #00a2d4; } .webuploader-pick-disable { opacity: 0.6; pointer-events:none; } .file-list{ width: 100%; } </style>
2 . 컴포넌트 내보내기
var fileUpload = require('./src/file_upload.vue'); module.exports = { fileUpload }
3. 데모 참조 방법
<template> <p> <el-card class="box-card"> <fileUpload ref="fileUpload" :ext="ext" :countLimit="5" :tip="tip">aaa</fileUpload> </el-card> </p> </template> <script> import {fileUpload} from '@/components/fileUpload/index.js' export default{ name: 'hello', components: {fileUpload}, data(){ return{ fileList: [], ext: 'png,jpg,jpeg,mp3,mp4,pdf', tip: '可上传png/jpg/jpeg/mp3/mp4/pdf,大小不超过200M' } }, created(){ }, methods: { getFileList: function(){ this.fileList = this.$refs.fileUpload.getFileList(); console.log(this.fileList); } } } </script>
4. 작업 렌더링
관련 추천:
자세한 설명 Bootstrap 파일 입력 파일 업로드 구성 요소의 예
Fine Uploader 파일 업로드 구성 요소 응용 프로그램 소개_PHP 튜토리얼
vue Alibaba Cloud 업로드 구성 요소 자세한 설명
위 내용은 webuploader 파일 업로드 컴포넌트 개발 예의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











1. 모듈을 사용하여 파일에 로그 출력: 로깅은 사용자 정의 수준 로그를 생성하고 지정된 경로에 로그를 출력할 수 있습니다. 로그 수준: 디버그(디버그 로그) = 5) {clearTimeout(시간) // 한 번에 모두 10번 획득한 경우 행이 비어 있음 로그 지우기 예약 작업}return}if(data.log_type==2){//(i=0;i에 대해 새 로그를 얻은 경우)

Caddy 소개 Caddy는 현재 Github에 38,000개 이상의 별이 있는 강력하고 확장성이 뛰어난 웹 서버입니다. Caddy는 Go 언어로 작성되었으며 정적 리소스 호스팅 및 역방향 프록시에 사용할 수 있습니다. Caddy에는 다음과 같은 주요 기능이 있습니다. Nginx의 복잡한 구성에 비해 원래 Caddyfile 구성은 매우 간단합니다. 기본적으로 자동화된 HTTPS 구성을 지원하고 HTTPS 인증서를 자동으로 적용할 수 있습니다. 수만 개의 사이트를 Go 언어로 작성하여 어디서나 실행할 수 있으며 메모리 안전성이 더욱 보장됩니다. 우선 CentO에 직접 설치해보겠습니다.

얼굴 차단 사격은 영상 속 인물을 가리지 않고 다수의 사격이 떠다니는 것처럼 보이도록 하여 마치 인물 뒤에서 떠다니는 것처럼 보이게 하는 것을 의미합니다. 기계 학습은 몇 년 동안 널리 사용되었지만 많은 사람들은 이러한 기능을 브라우저에서도 실행할 수 있다는 사실을 모릅니다. 이 기사에서는 기사 마지막 부분에 적용 가능한 몇 가지 시나리오를 소개합니다. 이 솔루션을 통해 몇 가지 아이디어를 얻을 수 있기를 바랍니다. mediapipeDemo(https://google.github.io/mediapipe/)는 주류 얼굴 차단 공세 주문형 업로드의 구현 원리를 보여줍니다. 비디오 서버 백그라운드 계산은 비디오 화면의 세로 영역을 추출하고 이를 svg로 변환합니다. 클라이언트가 비디오를 재생하는 동안 서버에서 svg를 다운로드하고 사격, 초상화와 결합합니다.

JavaAPI 개발에서 웹 서버 처리를 위해 Jetty7 사용 인터넷의 발전과 함께 웹 서버는 애플리케이션 개발의 핵심 부분이 되었으며 많은 기업의 초점이기도 합니다. 증가하는 비즈니스 요구를 충족하기 위해 많은 개발자가 웹 서버 개발에 Jetty를 사용하기로 선택했으며 그 유연성과 확장성은 널리 인정받고 있습니다. 이 기사에서는 We 용 JavaAPI 개발에서 Jetty7을 사용하는 방법을 소개합니다.

양식 유효성 검사는 웹 애플리케이션 개발에서 매우 중요한 링크로, 애플리케이션의 보안 취약성과 데이터 오류를 방지하기 위해 양식 데이터를 제출하기 전에 데이터의 유효성을 확인할 수 있습니다. Golang을 사용하여 웹 애플리케이션에 대한 양식 유효성 검사를 쉽게 구현할 수 있습니다. 이 기사에서는 Golang을 사용하여 웹 애플리케이션에 대한 양식 유효성 검사를 구현하는 방법을 소개합니다. 1. 폼 유효성 검사의 기본 요소 폼 유효성 검사를 구현하는 방법을 소개하기 전에 먼저 폼 유효성 검사의 기본 요소가 무엇인지 알아야 합니다. 양식 요소: 양식 요소는

우선, frp가 무엇인지에 대해 의문이 생길 것입니다. 간단히 말해서, frp는 인트라넷 침투 도구입니다. 클라이언트를 구성한 후 서버를 통해 인트라넷에 액세스할 수 있습니다. 이제 내 서버는 nginx를 웹 사이트로 사용했으며 포트 80은 하나만 있습니다. FRP 서버도 포트 80을 사용하려면 어떻게 해야 합니까? 쿼리 후에는 nginx의 역방향 프록시를 사용하여 이를 수행할 수 있습니다. 추가하려면: frps는 서버이고 frpc는 클라이언트입니다. 1단계: 서버에서 nginx.conf 구성 파일을 수정하고 nginx.conf의 http{}에 다음 매개변수를 추가합니다. server{listen80

웹 표준은 W3C 및 기타 관련 기관에서 개발한 일련의 사양 및 지침으로, HTML, CSS, JavaScript, DOM, 웹 접근성 및 성능 최적화를 포함하며, 이러한 표준을 따르면 페이지의 호환성이 향상됩니다. 접근성, 유지 관리성 및 성능. 웹 표준의 목표는 웹 콘텐츠가 다양한 플랫폼, 브라우저 및 장치에서 일관되게 표시되고 상호 작용할 수 있도록 하여 더 나은 사용자 경험과 개발 효율성을 제공하는 것입니다.

Cockpit은 Linux 서버용 웹 기반 그래픽 인터페이스입니다. 이는 주로 신규/전문가 사용자가 Linux 서버를 보다 쉽게 관리할 수 있도록 하기 위한 것입니다. 이 문서에서는 Cockpit 액세스 모드와 CockpitWebUI에서 Cockpit으로 관리 액세스를 전환하는 방법에 대해 설명합니다. 콘텐츠 항목: Cockpit 입장 모드 현재 Cockpit 액세스 모드 찾기 CockpitWebUI에서 Cockpit에 대한 관리 액세스 활성화 CockpitWebUI에서 Cockpit에 대한 관리 액세스 비활성화 결론 조종석 입장 모드 조종석에는 두 가지 액세스 모드가 있습니다. 제한된 액세스: 이는 조종석 액세스 모드의 기본값입니다. 이 액세스 모드에서는 조종석에서 웹 사용자에 액세스할 수 없습니다.
