📄 user_upload_face.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class user_upload_face : System.Web.UI.Page
{
int userid;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userid"] == null)
Response.Redirect("~/user/userLogin.aspx?url=" + Server.HtmlEncode(Request.RawUrl));
else
this.userid = Convert.ToInt32(Session["userid"]);
if (IsPostBack && FileUpload1.HasFile) uploadimg();
}
private string uploadimg()
{
Boolean fileOK = false;
String path = Server.MapPath("~/user/face/");
//检验是否指定后缀
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
fileOK = true;
}
//利用postedFile.ContentLength来判断文件长度,检查是否符合文件大小,250000就是250k字节
if (fileOK && FileUpload1.PostedFile.ContentLength < 1000000)
fileOK = true;
else
fileOK = false;
}
if (fileOK)
{
try
{
int id =this.userid ;
string filename = path +id+ ".jpg";
if (File.Exists(filename))
File.Delete(filename);
FileUpload1.PostedFile.SaveAs(filename );
string str="../user/face/"+id+".jpg";
Label1.Text = "<img src="+str+" width=200/>";
}
catch (Exception ex)
{
Label1.Text = ex.Message ;
}
}
else
{
Label1.Text = "无法上传的文件类型或文件大小超过250K";
}
return "";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -