最近在学习asp.net,做了一个上传图片的小例子:如图:
\n
\n
在站点建一个上传图片的文件夹:upFiles
\n
文件:upFile.aspx
\n
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”upFile.aspx.cs” Inherits=”upFile” %>
\n
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
\n
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>上传图片 http://www.dwww.cn</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:FileUpload ID=”fileLoad” runat=”server” />
<asp:Button ID=”btnUpFile” runat=”server” onClick=”btnUpFile_Click” Text=”上传” /><br />
<br />
只能上传图片文件。<br />
<br />
<br />
<asp:Image ID=”img” runat=”server” Height=”58″ Width=”182″ /></div>
</form>
</body>
</html>
文件:upFile.aspx.cs
\n
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;
\n
public partial class upFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.img.Visible = false;
}
protected void btnUpFile_Click(object sender, EventArgs e)
{
string fullFileName = this.fileLoad.PostedFile.FileName;
string fileName = fullFileName.Substring(fullFileName.LastIndexOf(“\\\\”) + 1);
string typeFileName = fullFileName.Substring(fullFileName.LastIndexOf(“.”) + 1);
if (typeFileName == “jpg” || typeFileName == “gif” || typeFileName == “png”)
{
this.fileLoad.SaveAs(Server.MapPath(“upFiles”) + “\\\\” + fileName);
this.img.ImageUrl = “upFiles/” + fileName;
this.img.Visible = true;
Response.Write (“上传成功”);
}
else
{
Response.Write(“<script>alert(‘错误的图片文件格式’);history.back();</script>”);
}
}
}
\n
可以实现简单的图片上传了,初学asp.net,写的不好,见笑了。
\n
作者:无情 出处:http://www.dwww.cn 转载请注明出处
\n