魏长东

weichangdong

pupload上传文件

因为涉及到跨域问题,放弃了uploadify,用这个实现了。

包含进来这个文件 plupload.full.min.js

<script>
 var uploader = new plupload.Uploader({
	runtimes : 'html5,flash,silverlight,html4',
	browse_button : 'file', // you can pass an id...
	container: document.getElementById('container'), // ... or DOM Element itself
	url : 'http://xxxx',
	flash_swf_url : '/assets/lib/pupload/js/Moxie.swf',
	silverlight_xap_url : '/assets/lib/pupload/js/Moxie.xap',
	
	filters : {
		max_file_size : '10mb',
		mime_types: [
			{title : "Image files", extensions : "jpg,gif,png"},
			{title : "Zip files", extensions : "zip"}
		]
	},

	init: {
		PostInit: function() {
			document.getElementById('filelist').innerHTML = '';

			document.getElementById('uploadfiles').onclick = function() {
				uploader.start();
				return false;
			};
		},

		FilesAdded: function(up, files) {
			plupload.each(files, function(file) {
				document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
			});
		},
        FileUploaded:function(up, file,res) {
            
            var re_data = eval('(' + res.response + ')');
            var  id = re_data.id;
            var  img_server_url = re_data.img_server_url;
            var  ok_url = img_server_url + '/crop/xl/c/'+id;
            //alert(ok_url)
            $("input[name='push_icon']").val(ok_url);
             $("#push_icon_show").attr('src',ok_url);
        },
		UploadComplete:function(up, file) {
			
		},
		UploadProgress: function(up, file) {
			document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
		},

		Error: function(up, err) {
		      alert(err.message);
			//document.getElementById('console').appendChild(document.createTextNode("\nError #" + err.code + ": " + err.message));
		}
	}
});

uploader.init();

function reloadPage()
{
	location.reload();
}
</script>

html code

               <!------upload img start------>
               <div id="filelist">Your browser doesn't have Flash, Silverlight or HTML5 support.</div>
<br />

<div id="container">
    <a id="file" href="javascript:;" class="btn btn-primary">Select</a> 
    <a id="uploadfiles" href="javascript:;" class="btn btn-primary">Upload</a>
	<a id="cancelfiles" href="javascript:reloadPage();" class="btn btn-primary">Cancel</a>
</div>
<!------upload img end------>

觉得这个插件 还是很好用的。