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上传图片至数据库并显示图片
<%@ 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上传图片至数据库并显示图片
相关文章推荐
- 告别.NET生成报表统计图的烦恼(ASP.NET Chart)
- Raspberry pi 之间的串口通信总结
- 大型高性能ASP.NET系统架构设计
- 【转载】ASP.NET线程安全与静态变量的生命周期浅谈
- ASP.NET 多线程 监控任务执行情况,并显示进度条
- RaspberryPi
- ASP.NET分页存储过程,解决搜索时丢失条件信息
- Asp.Net图片上传后缩略图空白
- Asp.Net 生成静态页并实现分页效果
- ASP.NET MVC5 实现网址伪静态
- ASP.NET—007:GridView数据的保存
- ASP.NET—007:GridView数据的保存
- ASP.NET—007:GridView数据的保存
- asp.net页面之间传递的方式
- ASP.NET—007:GridView数据的保存($.ajax实现前后台交互)
- [Asp.net]使用flexpaper+swftools大文件分页转换实现在线预览
- 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记
- ASP.Net 各类网站第一打开慢的问题解决方案
- 记录ASP.NET页面表单初始状态(主要是为了前台可以根据这个判断页面是否变动了)
- Js触发ASP.NET Validation控件的验证, 同时获取前台验证结果(不包括CustomValidator)