C#實現(xiàn)上傳照片到物理路徑,并且將地址保存到數(shù)據(jù)庫的小例子
效果:
思路:
首先,獲取圖片物理地址,然后進(jìn)行判斷將圖片保存到文件夾下,再將圖片的信息保存到數(shù)據(jù)庫。
數(shù)據(jù)庫:
create table image1
(
ID int identity(1,1) primary key,
ImageName varchar(100) ,
ImageType varchar(20),
ImagePath varchar(200)
)
代碼:
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" style="height: 21px">
</td>
</tr>
<tr>
<td style="width: 400px">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Label ID="label1" runat="server" ForeColor="Red"></asp:Label>
</td>
<td style="width: 80px">
<asp:Button ID="UploadButton" runat="server" Text="上傳圖片" OnClick="UploadButton_Click" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<br />
<br />
<asp:Image ID="Image1" runat="server" Height="118px" Width="131px" />
</td>
</tr>
</table>
</div>
</form>
</body>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace InExcelOutExcel
{
public partial class UpWord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
string SQLString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();
protected void UploadButton_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection sqlcon = new SqlConnection(SQLString))
{
string FullName = FileUpload1.PostedFile.FileName;//獲取圖片物理地址
FileInfo fi = new FileInfo(FullName);
string name = fi.Name;//獲取圖片名稱
string type = fi.Extension;//獲取圖片類型
if (type == ".jpg" || type == ".gif" || type == ".bmp" || type == ".png")
{
string SavePath = Server.MapPath("~\\excel");//圖片保存到文件夾下
this.FileUpload1.PostedFile.SaveAs(SavePath + "\\" + name);//保存路徑
this.Image1.Visible = true;
this.Image1.ImageUrl = "~\\excel" + "\\" + name;//界面顯示圖片
string sql = "insert into image1(ImageName,ImageType,ImagePath) values('" + name + "','" + type + "','~\\excel" + name + "')";
SqlCommand cmd = new SqlCommand(sql, sqlcon);
sqlcon.Open();
cmd.ExecuteNonQuery();
this.label1.Text = "上傳成功";
}
else
{
this.label1.Text = "請選擇正確的格式圖片";
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
相關(guān)文章
ASP.NET中實現(xiàn)Form表單字段值自動填充到操作模型中
這篇文章主要介紹了ASP.NET中實現(xiàn)Form表單字段值自動填充到操作模型中,本文模仿MVC模式中的自動映射表單了模型,使用泛型和反射實現(xiàn),需要的朋友可以參考下2015-06-06ASP.NET下將Excel表格中的數(shù)據(jù)規(guī)則的導(dǎo)入數(shù)據(jù)庫思路分析及實現(xiàn)
今天接到新的需求,要求將Excel表格中的數(shù)據(jù)顯示在頁面上個人想法:首先是規(guī)則的Excel數(shù)據(jù)導(dǎo)入,再有就是不規(guī)則的Excel數(shù)據(jù)導(dǎo)入,還有就是根據(jù)數(shù)據(jù)生成Excel2013-01-01asp.net通過Ajax UpdatePanel回傳后滾動條位置變更解決方法
用一個隱藏控件保存當(dāng)前scorll值。回傳回來后根據(jù)scroll的值在重新設(shè)置scroll。2010-06-06ASP.NET Core使用JWT認(rèn)證授權(quán)的方法
這篇文章主要介紹了ASP.NET Core使用JWT認(rèn)證授權(quán)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11.NetCore?Web?Api?利用ActionFilterAttribute統(tǒng)一接口返回值格式及問題解析
在實際項目開發(fā)過程中,統(tǒng)一API返回值格式對前端或第三方調(diào)用將是非常必要的,在.NetCore中我們可以通過ActionFilterAttribute來進(jìn)行統(tǒng)一返回值的封裝,對.NetCore?Web?Api?統(tǒng)一接口返回值格式相關(guān)知識感興趣的朋友一起看看吧2022-03-03ASP.NET漢字轉(zhuǎn)拼音 - 輸入漢字獲取其拼音的具體實現(xiàn)
這篇文章主要介紹了ASP.NET漢字轉(zhuǎn)拼音 - 輸入漢字獲取其拼音的具體實現(xiàn),需要的朋友可以參考下2014-02-02