一佳一

记录像1+1一样简洁的代码

导航

asp.net无刷新上传(带预览)

Posted on 2016-02-19 15:53  一佳一  阅读(595)  评论(0编辑  收藏  举报

1.有个图片

<img id="Image1" title="用于广告栏及图文框缩略图" width="150" height="100" />

2.存储路径

 <asp:HiddenField ID="HiddenField_Image1" runat="server" />

3.初始化js

<script type="text/javascript" src="Scirpt/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="Scirpt/ajaxupload.js"></script>

$(function () {
var hidimg = document.getElementById("HiddenField_Image1");
var btnImg = document.getElementById("Image1");
uploadFile(btnImg, hidimg);
});

4.上传代码

function uploadFile(btn, hidimg) {
var button = btn;
new AjaxUpload(button, {
action: 'TTHandler.ashx',
data: {},
name: 'myfile',
onSubmit: function (file, ext) {
if (!(ext && /^(jpg|JPG|png|PNG|gif|GIF)$/.test(ext))) {
alert("您上传的图片格式不对,请重新选择!");
return false;
}
},
// 开始上传文件
onSubmit: function (file, extension) {
$("#state").val("正在上传" + file + "..");
},
// 上传完成之后
onComplete: function (file, result) {
var remsg = result.split("|");
var name = remsg[1].split("\/");
if (remsg[0] == "1") {
$(btn).attr("src", remsg[1]);
$(hidimg).val(remsg[1]);
$("#state").val("上传成功..");
} else {
$.messager.alert("提示", remsg[2]);
}
}
});
}

 

完整下载路径

http://download.csdn.net/detail/jine515073/9436752