asp.net上传控件使用

时间:2024-01-12 08:03:26
asp.net上传控件使用
 protected void Button1_Click(object sender, EventArgs e)
{
string str = "";
if (FileUpload1.HasFile)
{
try
{
if (FileUpload1.PostedFile.ContentLength < 10240)
{ FileUpload1.SaveAs("e:\\" + FileUpload1.FileName);
str += "上传的文件名字为" + FileUpload1.FileName + "<br/>";
str += "文件来自" + FileUpload1.PostedFile.FileName + "<br/>";
str += "文件大小" + FileUpload1.PostedFile.ContentLength + "<br/>";
str += "文件的类型为" + FileUpload1.PostedFile.ContentType + "<br/>";
Label1.Text = "文件上传成功,相应信息如下:" + str;
}
else
{
Response.Write("<script>alert('文件大小超出范围!')</script>");
} }
catch (Exception ex)
{
Label1.Text = ex.Message;
}
} }
protected void Button2_Click(object sender, EventArgs e)
{
string name = FileUpload2.FileName;
string size = FileUpload2.PostedFile.ContentLength.ToString();
string type = FileUpload2.PostedFile.ContentType;
string path = Server.MapPath("images\\") + name;
string tp = name.Substring(name.LastIndexOf("."));
if (type.Substring(0, 5) == "image")
{
FileUpload2.SaveAs(path);
Label2.Text = "文件名:" + name + "<br/>" + "大小:" + size + "<br/>" + "文件类型:" + type + "<br/>" + "后缀:" + tp + "<br/>" + "保存路径:" + path + "<br/>";
}
}