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;
/**//// <summary>
/// 破晓之阳
/// 2007/12/07
/// http://user.qzone.qq.com/113605767
///开发环境 vs2005
/// </summary>
public partial class inputfile_inputfile : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void ButUpload_Click(object sender, EventArgs e)
...{
//string fillFileName = this.File1.PostedFile.FileName;
string fillFileName = this.File1.Value;//最好用上面的,会比较好。
//string fileName = fillFileName.Substring(fillFileName.LastIndexOf("\")+1);
string type = fillFileName.Substring(fillFileName.LastIndexOf(".")+1);
string typeS = type.ToLower();
int length = this.File1.PostedFile.ContentLength;
if (length > 100*1024)
...{
Response.Write("<script language='javascript'>alert('上传的文件不能大于100KB!!')</script>");
return;
}
else
...{
if (typeS == "bmp" || typeS == "gif" || typeS == "jpg")
...{
string time = System.DateTime.Now.ToString("yyyyMMddhhddssfff");
//this.File1.PostedFile.SaveAs(Server.MapPath("up") + "\" + fileName);
this.File1.PostedFile.SaveAs(Server.MapPath("up") + "\" + time + "." + type);
//this.Image1.ImageUrl = "up/" + fileName;
string str = Request.Url.AbsoluteUri;
string url = str.Substring(0, str.LastIndexOf('/')) + "/up/" + time + "." + type;
this.Image1.ImageUrl = url;//也可以用上面注释的一行替换下面的三行。
}
else
...{
Response.Write("<script language='javascript'>alert('你选择的图片格式错误!!')</script>");
}
}
}
}
aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="inputfile.aspx.cs" Inherits="inputfile_inputfile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>上传文件</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="File1" type="file" runat="server" />
<asp:Button ID="ButUpload" runat="server" Text="上传" OnClick="ButUpload_Click" /></div>
<div><asp:Image ID="Image1" runat="server" ImageUrl="~/inputfile/image/male.gif" /></div>
</form>
</body>
</html>
发表于 @ 2007年12月07日 23:27:00 | 评论( loading... ) | 举报| 收藏