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;
}
}