溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#如何實現上傳照片到物理路徑并且將地址保存到數據庫

發布時間:2021-05-17 11:00:13 來源:億速云 閱讀:708 作者:小新 欄目:開發技術

這篇文章主要介紹C#如何實現上傳照片到物理路徑并且將地址保存到數據庫,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

效果:

C#如何實現上傳照片到物理路徑并且將地址保存到數據庫

C#如何實現上傳照片到物理路徑并且將地址保存到數據庫

思路:

首先,獲取圖片物理地址,然后進行判斷將圖片保存到文件夾下,再將圖片的信息保存到數據庫。

數據庫:

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" >
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td >
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    &nbsp;<asp:Label ID="label1" runat="server" ForeColor="Red"></asp:Label>
                </td>
                <td >
                    <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);
            }
        }
    }
}

C#是什么

C#是一個簡單、通用、面向對象的編程語言,它由微軟Microsoft開發,繼承了C和C++強大功能,并且去掉了一些它們的復雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優雅的語法風格、創新的語言特性和便捷的面向組件編程從而成為.NET開發的首選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應用程序所需要的關鍵功能。

以上是“C#如何實現上傳照片到物理路徑并且將地址保存到數據庫”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女