當前位置:首頁 » 操作系統 » 把圖片路徑存入資料庫

把圖片路徑存入資料庫

發布時間: 2023-03-05 22:42:13

A. 如何將圖片路徑存入資料庫 圖片路徑是放在哪的路徑

圖片的路徑通常是放在伺服器里的,將路徑存入資料庫的話很簡單,在資料庫設置一個varchar型的欄位就可以了。 資料庫是通過伺服器進行連接的,不用上傳的;要圖片顯示出來的話,當然要讀出資料庫相應得欄位獲取字元串,也就是圖片的路徑。

B. 如何將圖片存到資料庫

通常對用戶上傳的圖片需要保存到資料庫中。解決方法一般有兩種:一種是將圖片保存的路徑存儲到資料庫;另一種是將圖片以二進制數據流的形式直接寫入資料庫欄位中。以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
string uppath="";//用於保存圖片上傳路徑
//獲取上傳圖片的文件名
string fileFullname = this.FileUpload1.FileName;
//獲取圖片上傳的時間,以時間作為圖片的名字可以防止圖片重名
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
//獲取圖片的文件名(不含擴展名)
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
//獲取圖片擴展名
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
//判斷是否為要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")
{
//將圖片上傳到指定路徑的文件夾
this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type);
//將路徑保存到變數,將該變數的值保存到資料庫相應欄位即可
uppath = "~/upload/" + dataName + "." + type;
}
二、將圖片以二進制數據流直接保存到資料庫:
引用如下命名空間:
using System.Drawing;
using System.IO;
using System.Data.sqlClient;
設計資料庫時,表中相應的欄位類型為iamge
保存:
//圖片路徑
string strPath = this.FileUpload1.PostedFile.FileName.ToString ();
//讀取圖片
FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123");
string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作資料庫語句根據需要修改
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
if (myComm.ExecuteNonQuery() > 0)
{
this.Label1.Text = "ok";
}
myConn.Close();
讀取:
...連接資料庫字元串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定從資料庫讀取出來的圖片的保存路徑及名字
string strPath = "~/Upload/zhangsan.JPG";
string strPhotoPath = Server.MapPath(strPath);
//按上面的路徑與名字保存圖片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//顯示圖片
this.Image1.ImageUrl = strPath;
採用倆種方式可以根據實際需求靈活選擇。

C. mysql資料庫可以存圖片路徑

可以。存圖片的列需要設置成blob、mediumblob或longblob等數據類型。
但是以前基本上不會把圖片直接存在資料庫里,因為資料庫里的數據是為了用來快速分析、快速存取的,圖片數據在mysql里既不能建立索引也不能和其他數據一起分析,存取速度和讀寫磁碟也沒什麼區別,每次備份、導入導出資料庫時還增加了數據量,降低了效率。所以一般都是把圖片存在系統里,然後把圖片的存放路徑放在資料庫里。

D. thinkphp 多圖上傳,如何把圖片路徑保存到資料庫

關於上傳文件:

推薦使用插件Uploadify!

使用方法:http://www.uploadify.com/documentation/

下載地址:http://www.uploadify.com/download/


關於多圖上傳:

  1. 在客戶端定義一個js變數,用於緩存每次非同步上傳圖片路徑;

  2. 最後提交表達數據的時候就把這些路徑集合保存到資料庫即可!

E. 如何將圖片存到資料庫中

一般圖片的處理都是上傳到伺服器然後將圖片的地址名稱依次保存在資料庫中,取出時按照地址取出就可以。直接用網上的圖片地址有的是可以的訪問,有的因為圖片加鎖,保留網上那個圖片地址最終是無法找到圖片的。

F. 圖片如何存入資料庫

通常對用戶上傳的圖片需要保存到資料庫中。解決方法一般有兩種:一種是將圖片保存的路徑存儲到資料庫;另一種是將圖片以二進制數據流的形式直接寫入資料庫欄位中。以下為具體方法:
一、保存圖片的上傳路徑到資料庫:
string uppath="";//用於保存圖片上傳路徑
//獲取上傳圖片的文件名
string fileFullname = this.FileUpload1.FileName;
//獲取圖片上傳的時間,以時間作為圖片的名字可以防止圖片重名
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
//獲取圖片的文件名(不含擴展名)
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
//獲取圖片擴展名
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
//判斷是否為要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")
{
//將圖片上傳到指定路徑的文件夾
this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type);
//將路徑保存到變數,將該變數的值保存到資料庫相應欄位即可
uppath = "~/upload/" + dataName + "." + type;
}
二、將圖片以二進制數據流直接保存到資料庫:
引用如下命名空間:
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
設計資料庫時,表中相應的欄位類型為iamge
保存:
//圖片路徑
string strPath = this.FileUpload1.PostedFile.FileName.ToString ();
//讀取圖片
FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123");
string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作資料庫語句根據需要修改
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
if (myComm.ExecuteNonQuery() > 0)
{
this.Label1.Text = "ok";
}
myConn.Close();
讀取:
...連接資料庫字元串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查詢語句根據需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定從資料庫讀取出來的圖片的保存路徑及名字
string strPath = "~/Upload/zhangsan.JPG";
string strPhotoPath = Server.MapPath(strPath);
//按上面的路徑與名字保存圖片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//顯示圖片
this.Image1.ImageUrl = strPath;
採用倆種方式可以根據實際需求靈活選擇。

熱點內容
紅米note擴展存儲卡 發布:2025-08-20 21:27:10 瀏覽:862
驗證你的電子郵件地址不能連接伺服器 發布:2025-08-20 21:27:09 瀏覽:63
存儲區是什麼意思 發布:2025-08-20 21:26:31 瀏覽:53
壓縮袋是什麼 發布:2025-08-20 20:48:27 瀏覽:618
伺服器減容會有什麼影響 發布:2025-08-20 20:40:23 瀏覽:150
我的世界怎麼聯伺服器 發布:2025-08-20 20:34:31 瀏覽:498
c語言編譯或解釋 發布:2025-08-20 20:27:17 瀏覽:601
vsm編程 發布:2025-08-20 20:16:31 瀏覽:913
腳本刷黑石塔 發布:2025-08-20 19:50:08 瀏覽:982
網上學編程可靠嗎 發布:2025-08-20 19:45:13 瀏覽:650