您好,登錄后才能下訂單哦!
小編給大家分享一下C#實(shí)現(xiàn)上傳照片到物理路徑的示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
C#實(shí)現(xiàn)上傳照片到物理路徑,并且將地址保存到數(shù)據(jù)庫(kù)的小例子
效果:
思路:
首先,獲取圖片物理地址,然后進(jìn)行判斷將圖片保存到文件夾下,再將圖片的信息保存到數(shù)據(jù)庫(kù)。
數(shù)據(jù)庫(kù):
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"> <p> <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> </p> </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 = "請(qǐng)選擇正確的格式圖片"; } } } catch (Exception ex) { Response.Write(ex.Message); } } } }
以上是“C#實(shí)現(xiàn)上傳照片到物理路徑的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。