1、Body标签中增加元素
<div class="m"><img src="~/Content/images/addp.png" width="100%" onclick="uploadLogoImage(this)"/></div> <input type="file" style="display: none" id="fileUpload" onchange="uploadImg(this)" />
2、编写上传方法
function uploadLogoImage(img) { $(‘#fileUpload‘).click(); } ///TODO:liandy 方法待修改 //图片上传 function uploadImg(obj) { var _form = $("<form></form>"); _form.append(obj); console.log(_form); _form.ajaxSubmit({ url: "/File/UploadImage", //url:staticFileRoot + "/FileUpload.ashx", type: "post", enctype: "multipart/form-data", contentType: "application/x-www-form-urlencoded; charset=utf-8", dataType: "json", success: function (data) { if ("0" == data.error) { $(‘#filePath‘).val(data.url); //删除.media的子元素 var media = $(".m").parent(); media.find("*").remove(); //增加.media的新的子元素 var closed = "<div class=‘closed‘><img src=‘/Content/images/closed.png‘ onclick=‘removeImg(this)‘ width=‘20‘ /></div>"; var m = "<div class=‘m‘><img src=‘"+data.url+"‘ width=‘100%‘ /></div>"; media.append(closed,m); } else { jsprint(error.msg, "", "error"); } } }); }
时间: 2024-12-19 12:28:14