js实现前台多张图片上传及预览

1.上传图片时简单预览和删除功能。预览时,不需传后台,上传打开的窗口中每次只能选择一张图片,每张图片对应一个input,如果图片被删除,对应的input也需删除,不影响要传后台参数。注:为了方便用到jquery,窗口中能多选文件,删除时还不知道怎么改input。html:

<form action="/***/correct"  method='post' enctype='multipart/form-data' >
	<div  class='upimgcontent' style="margin:0px auto;width:990px;">
		<div class="fileinput-wrap">
		<input type="file" name="files" id="doc0" imgid="img0" style="width:100px;"
 			οnchange="javascript:setImagePreviews()" />
	</div>
 		<div id="dd" style="width:990px;"></div> 
	</div>
	<button type="submit">提交</button>
</form>


javascript:
var magId=0;
function setImagePreviews(){
	var docobj=document.getElementById("doc"+magId);
	var dd=document.getElementById("dd");
	var fileList=docobj.files;	
	for( var i=0;i<fileList.length;i++){
		magId++;
		$(dd).append("<div class='img-wrap' imgid='img"+(magId-1)+"'><img id='img"+(magId-1)+"'/><span class=' closeimg"+(magId-1)+" close'>×</span></div>");
		$(".fileinput-wrap [imgid=img"+(magId-1)+"]")[0].style.position="absolute";
		$(".fileinput-wrap [imgid=img"+(magId-1)+"]")[0].style.left="-10000px";		
		$(".fileinput-wrap").append('<input type="file" name="files" id="doc'+magId+'" imgid="img'+magId+'" style="width:100px;" οnchange="javascript:setImagePreviews()" />');		
		$(".closeimg"+(magId-1)).on("click",function(e){
			var id=$(this).parents(".img-wrap").attr("imgid");
			$(this).parent().remove();
			$(".fileinput-wrap input[imgid="+id+"]").remove();
		})
		var imgObjPreview=document.getElementById("img"+(magId-1));
		if(docobj.files&&docobj.files[i]){
			imgObjPreview.style.display="block";
			imgObjPreview.style.width="100px";
			imgObjPreview.style.height="120px";
			imgObjPreview.src=window.URL.createObjectURL(docobj.files[i]);
		}else{
			//IE
			docobj.select();
			var imgsrc=document.selection.createRange().text;
			var localImageId=document.getElementById("img"+(magId-1));
			localImageId.style.width="100px";
			localImageId.style.height="120px";
			try{
				localIamgeId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
				localIamgeId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=imgsrc;
			}catch(e){
				alert("上传图片出错")
				return false;
			}
			imgObjPreview.style.display="none";
			document.selection.empty();
		}		
	}
	return true;
} 







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
主要用了Uploadify插件,还有AJAX。 兼容各种主流浏器:chrome Firefox IE ... IE7以上都兼容,IE6没试过。 因为Uploadify是用flash做的,所以如果浏器上面看不到"上"按钮,请先检查你的flash是否已安装。 压缩包里包含两个项目,一个是多图上的示例(不含数据库),另一个也是多图上的示例,不过这个可以修改每张图片的描述,所以用了SQL数据库。 项目中UploadHandler.ashx里面有等比例生成缩略图和裁剪图片的方法,如有需要可直接调用。 第一个项目和第二个项目都主要用Jquery AJAX Uploadify来完成上图片的功能。很简单的代码,不会复杂。如果不认识这三个东西的,建议百度或谷歌一下。 第二个项目用了linq to sql进行对SQL数据库的操作,包括上图片时对数据库进行插入数据的操作,删除图片时对数据库进行删除的操作,设置"封面图片"时对数据库的操作。 这个我是用ASP.NET建的项目,如果你是其他平台的也可以参考里面某些代码,如Uploadify上,Jquery对图片列表的操作。 其他的具体看项目,代码片段: PicUpload.js : //删除图片 function deletePic(n) { $.ajax({ type: "GET", url: "/api/UploadHandler.ashx?action=deletePic", data: "picName=" + n + "&ver=" + new Date().getTime(), dataType: "html", success: function (data) { if (data.length > 10) { $("li[id='p_" + n.replace(".jpg", "") + "']").remove(); } } }) } UploadHandler.ashx : //删除图片 public bool DeletePic(string pName) { string uploadPath = HttpContext.Current.Server.MapPath("~/Upload/" + pName); if (System.IO.File.Exists(uploadPath)) { System.IO.File.Delete(uploadPath); Album query = (from item in db.Album where item.Pname == pName select item).First(); db.Album.DeleteOnSubmit(query); db.SubmitChanges(); return true; } else return false; } 最后简单说下Uploadify,Uploadify的介绍网上都很多,也有很多的示例。我就说说功能,看下面的代码: $(document).ready(function () { $("#uploadify").uploadify({ 'uploader': '/js/jquery.uploadify-v2.1.0/uploadify.swf', 'script': '/api/UploadHandler.ashx', 'cancelImg': '/js/jquery.uploadify-v2.1.0/cancel.png', 'folder': '/Upload', 'queueID': 'fileQueue', 'auto': true, 'multi': true, 'fileDesc': '请选择.jpg .png .bmp .jpeg文件', 'fileExt': '*.jpg;*.png;*.bmp;*.jpeg;', 'onComplete': function (e, queueId, fileObj, data) { $("#photoListUl").append(data); $("#progressText2").hide(); document.getElementById('photoListUl').scrollTop = "99999"; }, 'onSelect': function (e, queueId, fileObj) { $("#progressText1").text(iSum++); $("#progressText2").show(); } }); }); function uploadFile() { jQuery('#uploadify').uploadifyUpload() } 可以限定选择上的文件类型,我这里是上图片. 可以限定要上的文件的大小,如限定为15M? 可以在上的时候添加参数或各种验证. 这些你在网上可以找到很多例子的,我在项目里就不添加上去了。 ----------------------------------------------------------------- 项目仅供参考。如有疑问可直接回复或企鹅: 514158268

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值