Git Product home page Git Product logo

ajaxfileupload's Introduction

#AjaxFileUpload

Introduction

Rewrite the most famouse ajax uplad api: jQuery.AjaxFileUpload in vanilla-js(pure js). You can use this api as a function and without need jQuery.js any more.

Main File

ajax_file_upload.js

ajax_cros_file_upload_es6.js //support webpack and used to cross-domian-upload

Usage

AjaxFileUpload(clickEl,{
        action: url,
        onChange: function(filename) {},
        onSubmit: function(filename) {
            return true;  //false to stop the submit
        },
        onComplete: function(filename, response) {} //response is a planed json object 'this' is the input element
    });

IMPORTANT: the input element must have a name property

Usage for cross-domain-upload

Client:

  import {AjaxFileUpload} from './ajax_cros_file_upload_es6'
  //init input element as same as common

Server Side: Your have to support "OPTIONS" method and Return Headers include 'Access-Control-Allow-Origin','Access-Control-Allow-Headers','Access-Control-Allow-Methods'

  $method = $_SERVER['REQUEST_METHOD'];
  if($method == "OPTIONS") {
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
    die();
  }

And then when end the upload function , response a page with script:

  window.parent.postMessage('{$msg}',"*");  //$msg is your feedback data, it is a string.

Excemple for uploading pictures.

/excemple

##License MIT

ajaxfileupload's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.