[JS]How to read local file in JS

2017-1-22 写技术

How to read local file in JS?

function jsReadFiles(files) {
	if (files.length) {
		var file = files[0];
		var reader = new FileReader();
		if (/text+/.test(file.type)){
                	reader.onload = function(){
				$('body').append('<pre>' + this.result + '</pre>');
			}
			reader.readAsText(file);
		}else if(/image+/.test(file.type)) {
			reader.onload = function() {
				$('body').append('<img src="' + this.result + '"/>');
			}
			reader.readAsDataURL(file);
		}
	}
}

 

标签: JS

发表评论:

Powered by anycle 湘ICP备15001973号-1