客户端:
<asp:FileUpload ID="fuPhoto" οnchange="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;
}
}
Fileupload选择文件后自动上传
最新推荐文章于 2024-07-21 03:01:07 发布