<|start_filename|>example/java/h5uploader/src/com/h5uploader/demo/UploaderServlet.java<|end_filename|>
package com.h5uploader.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;


@MultipartConfig
public class UploaderServlet extends HttpServlet {
	
	public void service(HttpServletRequest req, HttpServletResponse res) 
			throws IOException, ServletException {
		
		res.setContentType("text/html;charset=UTF-8");

	    // Create path components to save the file
	    final String path = "/Users/ziv/Desktop/";
	    final Part filePart = req.getPart("myfile0");
	    final String fileName = getFileName(filePart);

	    OutputStream out = null;
	    InputStream filecontent = null;
	    final PrintWriter writer = res.getWriter();
	    
	    try {
	        out = new FileOutputStream(new File(path + File.separator
	                + fileName));
	        filecontent = filePart.getInputStream();

	        int read = 0;
	        final byte[] bytes = new byte[1024];

	        while ((read = filecontent.read(bytes)) != -1) {
	            out.write(bytes, 0, read);
	        }
	        writer.println("New file " + fileName + " created at " + path);
	       
	    } catch (FileNotFoundException fne) {
	        writer.println("You either did not specify a file to upload or are "
	                + "trying to upload a file to a protected or nonexistent "
	                + "location.");
	        writer.println("<br/> ERROR: " + fne.getMessage());

	        
	    } finally {
	        if (out != null) {
	            out.close();
	        }
	        if (filecontent != null) {
	            filecontent.close();
	        }
	        if (writer != null) {
	            writer.close();
	        }
	    }
	}
	
	private String getFileName(final Part part) {
	    final String partHeader = part.getHeader("content-disposition");
	    for (String content : part.getHeader("content-disposition").split(";")) {
	        if (content.trim().startsWith("filename")) {
	            return content.substring(
	                    content.indexOf('=') + 1).trim().replace("\"", "");
	        }
	    }
	    return null;
	}

}


<|start_filename|>example/expressjs/server.js<|end_filename|>
var express = require('express');
var multer = require('multer');
var app = express();
var done = false;

app.use(express.static('./public'));

// respond with "hello world" when a GET request is made to the homepage
app.post('/upload',[multer({dest: './uploads/'}), function(req, res) {
    try {
        console.log(req.body.myfile);
        console.log(req.files);
        res.json(200);
    } catch (e) {
        console.log(e);
    }
}]);

app.get("/", function(req, res, next) {
    res.redirect('/uploader.html');
});

var server = app.listen(3000, function() {
    console.log('Example app listening at http://%s:%s', '127.0.0.1', 3000);
});

<|start_filename|>example/java/h5uploader/webapp/index.jsp<|end_filename|>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>Test</title>
</head>

<body>
    <h1>H5 file upload demo</h1>
    <input type="file" id="myfile0" value="" name="myfile0" multiple="multiple"/>
    <input type="file" id="myfile1" value="" name="myfile1" multiple="multiple"/>
    <button id="uploader">Upload</button>
    <script type="text/javascript" src="js/h5uploader.js"></script>
    <script type="text/javascript">
    var uploader = document.getElementById('uploader');
    uploader.addEventListener("click", function(e) {

        H5Uploader.upload({
            action: 'upload',
            id: 'myfile0',
            size: {
                max: 5000, // Kb 5MB
                valide: function(e) {
                    console.log(e);
                }
            }, // MB
            type: {
                name: 'csv',
                valide: function(e) {
                    console.log(e);
                }
            },
            progress: function() {
                var p = document.createElement('p');
                p.innerHTML = "uploading";
                p.id = "loading";
                document.body.appendChild(p);
            },
            success: function(data) {
                alert(data);
                if (data && data == 200) {
                    document.getElementById('loading').innerHTML = "The file upload successfully!";
                    alert("The file upload successfully.");
                }
            },                        
            fail: function(data) {

            }
        });

    }, false);
    </script>
</body>

</html>

<|start_filename|>example/sails/api/controllers/UploaderController.js<|end_filename|>
module.exports = {

    index : function(req, res) {
        return res.view("app/demo");
    },

    upload: function(req, res) {
        console.log("file upload....");
        req.file('myfile').upload(function(err, file) {
            sails.log.error(err);
            // 发送批处理请求
            console.log(file);
        });
        return res.ok();
    }
};

<|start_filename|>example/sails/assets/js/h5uploader.js<|end_filename|>
/**!
 * h5uploader.js
 * This is a simple file upload plugin depends on HTML5. 
 * You can use it to mordern browser.
 * 
 * version: 1.0
 * Copyright 2015, Ziv | http://imziv.com
 * Released under the MIT License
 * https://github.com/wewoor/h5uploader
 **/

(function(window) {

    window.H5Uploader = (function() {

        function createXhr() {
            if (typeof XMLHttpRequest != "undefined") {
                return new XMLHttpRequest();
            } else if (typeof ActiveXObject != "undefined") {
                if (typeof arguments.callee.activeXString != "string") {
                    var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"];
                    for (var i = 0, len = versions.length; i < len; i++) {
                        try {
                            new ActiveXObject(versions[i]);
                            arguments.callee.activeXString = versions[i];
                            break;
                        } catch (e) {
                            throw new Error('Create XHR ActiveXObject error.' + e);
                        }
                    }
                }
                return new ActiveXObject(arguments.callee.activeXString);
            } else throw new Error('No XHR object avaliable.' + e);
        }

        return {
            upload: function(literals) {
                if (Object.prototype.toString.call(literals) !== '[object Array]') {
                    this.handUpload(literals);
                } else {
                    for (var i = 0; i < literals.length; i++) {
                        this.handUpload(literals[i]);
                    }
                }
            },

            handUpload: function(literals) {

                if (literals.action === undefined) {
                    throw new Error('The upload action address option is undefined.');
                }

                var xhr = createXhr();
                xhr.open("POST", literals.action, true);
                xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4) {
                        var body = xhr.responseText;
                        if (xhr.status >= 200 && xhr.status < 300 ||
                            xhr.status == 304) {
                            if (literals.success) {
                                literals.success(body);
                            }
                        } else {
                            if (literals.fail) {
                                literals.fail(body);
                            }
                        }
                    }
                };
                var data = new FormData();
                if (!literals.id) {
                    throw new Error('The upload id option is undefined.');
                }
                var file = document.getElementById(literals.id);
                if (!file) {
                    throw new Error('The upload file element is undefined::id:' + literals.id);
                }
                var name = file.getAttribute('name');
                if (!name) {
                    throw new Error('The upload file input name is undefined.');
                }

                if (literals.size) { // Check file Size
                    var evt = this.checkSize(file.files, literals.size.max);
                    if (evt) {
                        if (literals.size.valide) literals.size.valide(evt);
                        throw new Error('The upload file size exceed max value.');
                    }
                }

                if (literals.type) { // Check file type
                    var evt1 = this.checkType(file.files, literals.type.name);
                    if (evt1) {
                        if (literals.type.valide) literals.type.valide(evt1);
                        throw new Error('The upload file type is error.');
                    }
                }

                if (literals.progress) { // Progress
                    literals.progress();
                }

                for (var i = 0; i < file.files.length; i++) {
                    data.append(name, file.files[i]);
                }

                try {
                    xhr.send(data);
                } catch (e) {
                    throw new Error(e);
                }
            },

            // Validate file size
            checkSize: function(file, size) {
                for (var i = 0; i < file.length; i++) {
                    if(file[i].size > size * 1024) { // bytes
                        return file[i];
                    }
                }
            },

            // Validate file type
            checkType: function(file, type) {
                for (var i = 0; i < file.length; i++) {
                    var arr = file[i].name.split(".");
                    if (type.indexOf(arr[arr.length - 1]) === -1) {
                        return file[i];
                    }
                }
            }
        };
    })();

})(window);