您的位置:首页 > 编程语言 > ASP

asp.net上传图片并显示

2014-03-29 13:45 337 查看
前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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>

<br />

<img src="images/C++图片.JPG" width="120px" height="100px" /><br />

<br />

</div>

<asp:FileUpload ID="FileUpload1" runat="server" />

<br />

<br />

<br />

<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="保存" />

<br />

<br />

<br />

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"

Text="跳到显示图片的页面" />

<br />

<br />

<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />

</form>

</body>

</html>

后台:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication1

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnSave_Click(object sender, EventArgs e)

{

string name = FileUpload1.FileName;

//获取文件名

string type = name.Substring(name.LastIndexOf(".") + 1);

//获取文件类型

string ipath = Server.MapPath("images") + "\\" + name;

//获取文件路径

string wpath = "images\\" + name;

//[color=red]设置文件保存相对路径 (这里的路径起始就是我们存放图片的文件夹名)[/color]

string query1 = "insert into Images values ('" + wpath + "')";

if (type == "jpg" || type == "jpeg" || type == "JPEG" || type == "JPG" || type == "gif" || type == "bmp" || type == "png")

{

FileUpload1.SaveAs(ipath);

//服务器保存路径

//sqlHelper.ExecterNonQuery(query1);

}

}

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("~/showPicPage.aspx");

}

protected void Button2_Click(object sender, EventArgs e)

{

Response.Redirect("~/showPicPage.aspx");

}

}

}

参考:http://developer.51cto.com/art/200907/138274.htm asp.net上传图片至数据库并显示图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: