实际例子(已上传图片为例)
protected void UpImg_Click(object sender, EventArgs e)//上传图片
{
if (FileUpload1.PostedFile.ContentLength < 500000)//100000为100K
{
string fileFullname = this.FileUpload1.FileName;
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
{
this.FileUpload1.SaveAs(Server.MapPath("../upload") + "\\" + dataName + "." + type);
string ProImg = "upload/" + dataName + "." + type;
this.txtProImg.Text = "upload/" + dataName + "." + type;
}
else
{
Response.Write("");
}
}
else
{
Response.Write("");
}
}
web.config配置
web.config文件的内容大体如此:
注意在文件中的位置,
讲解:
//cookieless="false",不使用cookies
//timeout="20",会话时间为20分钟,单位是分钟,这里可自行修改
//站点默认上传的最大文件
FileUpload1.PostedFile.ContentLength < 2097151 小于2M?
修改machine.config试试