iframe and html5 file uploader

Overview

Upload


spm package

iframe and html5 uploader.

演示

查看演示,你需要 clone 一份代码:

$ git clone git://github.com/aralejs/upload
$ cd upload
$ npm install
$ spm install
$ node server.js

然后访问:http://localhost:8000/demo.html

Attributes

var uploader = new Uploader({
    trigger: '#upload-icon',
    name: 'image',
    action: '/upload',
    accept: 'image/*',
    data: {'xsrf': 'hash'},
    multiple: true,
    error: function(file) {
        alert(file);
    },
    success: function(response) {
        alert(response);
    },
    progress: function(event, position, total, percent, files) {
        console.log(percent);
    }
});

trigger element|selector

trigger 唤出文件选择器,可以是:

- 选择器
- element
- jQuery Object

name string

name 即为 <input name="{{name}}"> 的值,即上传文件时对应的 name。

action url

action 为 <form action="{{action}}"> 的值,表单提交的地址。

accept string

支持的文件类型,比如 image/\* 为只上传图片类的文件。可选项。

multiple boolean

是否支持多文件上传。默认为 false。

data object

随表单一起要提交的数据。

error function

上传失败的回调函数。

success function

上传成功的回调函数。

progress function

上传的进度回调,不支持 IE9-。回调的参数分别为 ajaxEvent, 当前上传字节,总字节,进度百分比和当前文件列表。

Methods

链式调用:

var uploader = new Uploader({
    trigger: '#upload-icon',
    name: 'image',
    action: '/upload',
    data: {'xsrf': 'hash'}
}).success(function(response) {
    alert(response);
}).error(function(file) {
    alert(file);
});

Data API

<a id="upload" data-name="image" data-action="/upload" data-data="a=a&b=b">Upload</a>
<script>
var uploader = new Uploader({'trigger': '#upload'});
// more friendly way
// var uploader = new Uploader('#upload');
uploader.success(function(response) {
    alert(response);
});
</script>

Advanced

Demo in API section could not be controlled. When you select a file, it will be submitted immediately. We can broke the chain with change:

var uploader = new Uploader({
    trigger: '#upload-icon',
    name: 'image',
    action: '/upload',
    data: {'xsrf': 'hash'}
}).change(function(files) {
    for (var i=0; i<files.length; i++) {
        console.log('you are selecting ' + files[i].name + ' Size: ' + files[i].size);
    }
    // Default behavior of change is
    // this.submit();
}).success(function(response) {
    alert(response);
});

Now you should handle it yourself:

$('#submit').click(function() {
    uploader.submit();
});

Show Progress

It is impossible to show progress, but you can make a fake prgress.

var uploader = new Uploader({
    trigger: '#upload-icon',
    name: 'image',
    action: '/upload',
    data: {'xsrf': 'hash'},
    progress: function(e, position, total, percent, files) {
      $('#progress').text('Uploading ... ' + percent + '%');
    }
}).change(function(files) {
    // before submit
    $('#progress').text('Uploading ...');
    this.submit();
}).success(function(response) {
    $('#progress').text('Success');
    alert(response);
});

Seajs Hint

Load uploader with seajs:

seajs.use('upload', function(Uploader) {
    var uploader = new Uploader({
    });
});

Changelog

2013-12-10 1.1.0

  1. Add upload progress for html5 uploader
  2. change function argument change to a files list.
  3. fix multiple attribute.
  4. fix this in change function
  5. fix FileList object in ie 9-

2013-07-18 1.0.1

  1. Support choosing the same file for uploader
  2. Fix memory leaks for FormData

2013-06-25 1.0.0

Combine iframe and html5 uploader.

IE hint

blueimp/jQuery-File-Upload#1795

Comments
  • IE8:上传到七牛的问题

    IE8:上传到七牛的问题

    coffeescript代码如下:

       @uploader = new Uploader
          name: 'file'
          size: 2048000
          trigger: @$("#btn-upload")
          action: "http://upload.qiniu.com"
          data: {token: @upload_token}
          accept: 'image/*'
          multiple: false
      .success (data) =>
          console.log data
    

    ff,chrome,IE9+都没问题,在IE8中,上传文件后,出现下载文件提示,下载的文件内容就是七牛返回的内容

    在demo.html中尝试 action: '/' 正常,action: '//upload.qiniu.com' 提示下载文件

    opened by radision 4
  • Success response JSON is not work

    Success response JSON is not work

    JS:

    var FBUpload = new Uploader({
        trigger: '#FBUpload',
        name: 'file',
        accept: 'image/*',
        action: '/index.php?m=index&c=bingfa&a=upload',
    }).success(function(r) {
        console.log('r is: ' + r);
        console.log('code is: ' + r['code']);
    });
    

    Console:

    r is: {"code":1,"msg":null,"response":{"name":"\u4ea7\u54c1\u8fd0\u8425.jpg","
    image":"2\/2013\/08\/13\/34b88948e2192.jpg"},"ts":1376357267}
    
    code is: undefined
    
    opened by billwing 4
  • 1.2.0版本依赖spm-jquery造成webpack打包时jquery代码冗余的问题

    1.2.0版本依赖spm-jquery造成webpack打包时jquery代码冗余的问题

    目前npm上的最新版本是1.2.0,其中package.json文件中声明的dependencies如下: image

    实际项目中依赖了jquery2.1.4,在webpack打包的时候,存在同时把jquery2.1.4和spm-jquery都打进去的情况,有点疑问:

    1. spm-jquery是否是原生的jquery1.7.2代码,有否做过改动?
    2. arale-upload是否支持jquery2.1.4?
    3. github上最新的package.json文件把dependencies依赖写到了spm对象中,理论上应该解决了webpack打包的这个问题,是否能够升级一个版本? 4.如果短时间无法升级,是否有其他webpack config层面的解决方案?
    opened by shanggqm 2
  • ie8 下iframe 返回json问题

    ie8 下iframe 返回json问题

    会返回

    {json: "json"}

    182行: response = $(this).contents().find("body").html(); 建议修改为 response = $(this).contents().find("body").text();

    opened by huangzhonghuai 0
  • 动态计算input控件高度

    动态计算input控件高度

    form表单有动态计算,可表单下面的input文件控件没有动态计算 在如下场景会出现问题 dom元素没有确定位置而先new一个对象后,form表单会自动定位但由于input控件height=0造成还是无法上传文件 例如弹窗上传 先new一个对象可以避免多次创建dom元素和绑定事件 当弹窗位置确定后form表单位置正确,input控件height不正确

    opened by aofong 0
  • 如果上传按钮在初始化之前是隐藏的,会出现触发选择文件按钮的高度问题

    如果上传按钮在初始化之前是隐藏的,会出现触发选择文件按钮的高度问题

    如果我的上传按钮最开始是隐藏的,outerHeight()只能得到按钮的border+padding的高度,在按钮展示以后,当鼠标移动到上传按钮,会执行一下代码:

    // bind events
      Uploader.prototype.bind = function() {
        var self = this;
        var $trigger = $(self.settings.trigger);
        $trigger.mouseenter(function() {
          self.form.css({
            top: $trigger.offset().top,
            left: $trigger.offset().left,
            width: $trigger.outerWidth(),
            height: $trigger.outerHeight()
          });
        });
        self.bindInput();
      };
    

    这里重新计算了form的高度,所以form高度不会出现问题,但是没有重新计算触发选择文件按钮的高度,导致按钮不容易点击到(高度太小),这个地方应该重新计算高度!

    opened by lvshuang 0
The all-in-one image uploader for Taio and JSBox.

Image Uploader The all-in-one image uploader for Taio and JSBox. Features Many cloud services are supported Many ways to pick images Image tools like

Ying Zhong (Inactive) 39 Jan 3, 2023
A lightweight extension to automatically detect and provide verbose warnings for embedded iframe elements in order to protect against Browser-In-The-Browser (BITB) attacks.

Enhanced iFrame Protection - Browser Extension Enhanced iFrame Protection (EIP) is a lightweight extension to automatically detect and provide verbose

odacavo 16 Dec 24, 2022
A postMessage bridge to connect to dapps loaded into an iframe.

cardano-dapp-connector-bridge A postMessage bridge to connect to dApps loaded into an iframe. Motivation In April 2022, browser extensions are the onl

Tastenkunst GmbH 15 Oct 11, 2022
A simple javascript utility library to include partial html (iframe alternate) without a framework or jQuery.

alt-iframe A simple javascript utility library to include partial html (iframe alternate) without a framework or jQuery. <!doctype html> <html lang="e

FrontEndNeo 17 Dec 30, 2022
A MagicMirror² module which embeds multiple other websites with iframe or webview

MMM-EmbedURL This is a MagicMirror² module which embeds other websites either by "iframe" (default), "webview" or a custom HTML-element to your mirror

Thomas Hirschberger 6 Dec 18, 2022
A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil ?? A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Ben Smith 21 Jun 20, 2022
Serve file server with single zip file as file system in Deno.

zipland Serve file server with one-single zip file in Deno. Support zip just zip32 with deflated or uncompressed serving plaintext deflate Examples Yo

Yongwook Choi 18 Nov 2, 2022
Feel free to create new file, don't hesitate to pull your code, the most important thing is that the file name here must match your nickname so that file does not conflict with other people.

Hacktoberfest Indonesia Apa Itu Hacktoberfest ? Hacktoberfest adalah acara tahunan yang bertujuan untuk mendorong berkontribusi kedalam ekosistem open

Juan Daniel 5 Dec 15, 2022
CLI utility that parses argv, loads your specified file, and passes the parsed argv into your file's exported function. Supports ESM/TypeScript/etc out of the box.

cleffa CLI tool that: Parses argv into an object (of command-line flags) and an array of positional arguments Loads a function from the specified file

Lily Scott 9 Mar 6, 2022
File Hider - This is a plugin for Obsidian that allows hiding specific files and folders from the file explorer

File Hider - This is a plugin for Obsidian that allows hiding specific files and folders from the file explorer

Oliver 24 Dec 16, 2022
An enhanced HTML 5 file input for Bootstrap 5.x/4.x./3.x with file preview, multiple selection, and more features.

bootstrap-fileinput An enhanced HTML 5 file input for Bootstrap 5.x, 4.x, and 3.x with file preview for various files, offers multiple selection, resu

Kartik Visweswaran 5.2k Jan 3, 2023
With this File Manager prepared for PHP/Js, you can perform all file operations on your server without any problems.

FileManager With this File Manager prepared for PHP/Js, you can perform all file operations on your server without any problems. Instead of downloadin

Tanzer Demir 4 Sep 23, 2022
A file manager plugin for logseq(Search unused assets file)

logseq-plugin-file-manager Search files from assets and draws but not used in journals or pages. Please backup files before operation, and before dele

Hayden Chen 17 Dec 23, 2022
Node.js library that provide a cache for file metadata or file content.

@file-cache A cache library for file metadata or file content. It is useful for process that work a given series of files and that only need to repeat

azu 16 Aug 6, 2022
A Leaderboard app that pulls from a LeaderboardGameAPI and allows users to view recent scores and add more scores to the score board. Built with HTML5, CSS, JavaScript, and Webpack.

Leaderboard A Leaderboard app that pulls from a LeaderboardGameAPI and allows users to view recent scores and add more scores to the score board. Buil

Steven Ntakirutimana 5 Dec 21, 2022
Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.

Phaser - HTML5 Game Framework Phaser is a fast, free, and fun open source HTML5 game framework that offers WebGL and Canvas rendering across desktop a

Richard Davey 33.4k Jan 7, 2023
Simple To-do list build using HTML5, CSS3, JavaScript and Webpack. You can add new tasks, remove and edit them.

Simple To-do list build using HTML5, CSS3, JavaScript and Webpack. You can add new tasks, remove and edit them.

Esteban Munoz 6 Mar 5, 2022
This is Basic calculator. This is made up of HTML5,CSS and JAVASCRIPT.

<title>BasicCalculator</title> <style> @media screen and (max-width : 574px){ h2{ font-size: large; }} @media screen and (max-width : 430px){ h2{ font

Sanjay soni 1 Dec 22, 2021
This Project is made with HTML5, CSS3, ReactJS, Axios, MetaMask, thirdweb, Rinkeby Test Network, Web 3.0 Technologies, and OpenSea API.

Abstract Collections This Project is made with HTML5, CSS3, ReactJS, Axios, MetaMask, thirdweb, Rinkeby Test Network, Web 3.0 Technologies, and OpenSe

Shobhit Gupta 34 Jan 4, 2023