当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET让FileUpload控件支持浏览自动上传功能的解决方法

ASP.NET让FileUpload控件支持浏览自动上传功能的解决方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

上海教科院实验小学,弱碱性食物,板野友美 金在中

asp.net的fileupload控件默认是不支持服务端的onchange事件的,此时可以用一种变通的方法来实现这一功能。
这就需要借用客户端的onchange事件,调用__dopostback方法来用linkbutton的onclick事件模拟一个事件触发的过程,具体代码如下:

客户端:

<asp:fileupload id="fuphoto" onchange="javascript:__dopostback('lbuploadphoto','')" runat="server" tooltip="选择图片" />
<asp:linkbutton id="lbuploadphoto" runat="server" onclick="lbuploadphoto_click"></asp:linkbutton>

后台代码:

//自动上传事件
protected void lbuploadphoto_click(object sender, eventargs e)
{
fileupload();
}
//从控件上传文件
public void fileupload()
{
if (fuphoto.postedfile != null && fuphoto.postedfile.contentlength > 0)
{
string ext = system.io.path.getextension(fuphoto.postedfile.filename).tolower();
if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif")
{
return;
}
string filename = "image_" + datetime.now.tostring("yyyymmddhhmmss") + ext;
string path = "./uploadphoto/" + filename;
fuphoto.postedfile.saveas(server.mappath(path));
response.redirect("imagecut.aspx?picurl=" + server.urlencode(path));
}
else
{
//do some thing;
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网