當前位置:首頁 » 文件傳輸 » net操作blob上傳圖片
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

net操作blob上傳圖片

發布時間: 2022-04-27 09:17:13

❶ NET怎麼對oracle的blob欄位進行寫操作

這個很容易理解的
首先創建一個目錄:
create or replace directory mydir as 'd:';
declaredest_loc blob;--指定要讀入的文件,在mydir目錄下的
src_loc bfile := bfilename('MYDIR', 'ace_introe.jpg'); --目錄名字一定要大寫
amount integer := 4000;begin--插入記錄,
同時返回blob的locator
insert into testlobvalues(2,empty_blob()) returning b_l into dest_loc;--打開
bfiledbms_lob.open(src_loc, dbms_lob.lob_readonly);--獲得文件長度select dbms_lob.getlength(src_loc) into amount from al;--打開要寫入記錄的blob
locatordbms_lob.open(dest_loc, dbms_lob.lob_readwrite);--直接把文件load到blob欄位
dbms_lob.loadfromfile(dest_loc, src_loc,amount);--關閉相應的locatordbms_lob.close(dest_loc);
dbms_lob.close(src_loc);
commit;
end;
/

上傳照片的時候怎樣轉化為流保存到資料庫的欄位為BLOB欄位中

你把文件的的名稱保存到資料庫就可以了。
然後在頁面上<img scr='Image/<%Eval("資料庫圖片的欄位")#>'
現在是不會把圖片什麼放到資料庫 大家都同時訪問的時候讀取圖片(以二進制流的方式),
希望你能看懂我的話。

❸ .net上傳圖片和讀取圖片

using System;
using System.Drawing;
/// <summary>
/// 功能:文件上傳
/// </summary>
namespace czlib
{
public class UpFile
{
#region------上傳屬性-------
private string _path = @"\UpFile\";//上傳類型
private string _fileType = czlib.weblib.adminlib.web_up();//上傳類型
private int _sizes = 2097152;//傳文件的大小KB(2097152=2MB)
private int _W = 120;//寬300
private int _H = 100;//高300
private bool _smaMap = false;//是否生成縮略圖
private int _mode = 0;//生成方式
/// <summary>
/// 上傳保存路徑
/// </summary>
public string Path
{
set
{
_path = @"\" + value + @"\";
}
}
/// <summary>
/// 允許上傳大小
/// </summary>
public int Sizes
{
set
{
_sizes = value * 1024;//轉為位元組byte
}
}
/// <summary>
/// 允許上傳類型
/// </summary>
public string FileType
{
set
{
_fileType = value;
}
}
/// <summary>
/// 縮略圖寬度
/// </summary>
public int W
{
set { _W = value; }
}
/// <summary>
/// 縮略圖高度
/// </summary>
public int H
{
set { _H = value; }
}
public bool smaMap
{
set { _smaMap = value; }
}
/// <summary>
/// 縮略圖生成方式
/// </summary>
public int mode
{
set { _mode = value; }
}
#endregion
#region-------上傳文件方法------------
/// <summary>
/// 上傳文件
/// </summary>
/// <param name="ful">FileUpload控制項名稱</param>
/// <returns>保存路徑</returns>
public string fileSaveAs(System.Web.UI.WebControls.FileUpload ful)
{
try
{
if (ful.PostedFile.ContentLength == 0) { throw new ApplicationException("請選擇上傳文件!"); }
//LD_2008792347263759274.jpg
System.Random random = new Random();
String modifyFileName = "LD_"+DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + random.Next(1000, 9999);
String uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + _path;
String sourcePath = ful.PostedFile.FileName;
String tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1);
int strLen = ful.PostedFile.ContentLength;
if (strLen > _sizes)
{
throw new ApplicationException("上傳的圖片不能大於" + _sizes/1024 + "KB");
}
if (_fileType.IndexOf(tFileType + "|") == -1)
{
throw new ApplicationException("目前本系統只能上傳格式為:" + _fileType);
}
//目錄是否存在
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadFilePath);
if (!dir.Exists)
{
dir.Create();
}
String filePath = uploadFilePath + modifyFileName + "." + tFileType;
ful.SaveAs(filePath);
if (_smaMap)
{
MakeThumbnail(filePath, uploadFilePath + "small_" + modifyFileName + "." + tFileType);
}
filePath = _path + modifyFileName + "." + tFileType;
return filePath.Replace("\\", "/");
}
catch (Exception ex)
{
throw new ApplicationException("上傳時發生錯誤:" + ex.ToString());
}
}
#endregion
#region---------生成縮略圖方法--------------
/// <summary>
/// 生成縮略圖
/// </summary>
/// <param name="originalImagePath">實際路徑</param>
/// <param name="thumbnailPath">縮略圖存放實際路徑</param>
public void MakeThumbnail(String originalImagePath, String thumbnailPath)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = _W;
int toheight = _H;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (_mode)
{
case 1://指定寬,高按比例
toheight = originalImage.Height * _W / originalImage.Width;
break;
case 2://指定高,寬按比例
towidth = originalImage.Width * _H / originalImage.Height;
break;
case 3://指定高寬裁減(不變形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * _H / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default://指定高寬縮放(可能變形)
break;
}
//新建一個bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一個畫板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空畫布並以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置並且按指定大小繪制原圖片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存縮略圖
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception ex)
{
throw new ApplicationException("成生縮略圖錯誤:" + ex.Message);
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
#endregion
}
} 這是上傳並生成縮略圖的代碼,至於瀏覽就簡單了。自己寫寫就可以了

❹ C#+Asp.net開發用FileUpload上傳圖片時遇到以下問題:

我看你下面發的圖片,資料庫操作應該沒出問題吧
然後上傳圖片的路徑
比如/upload/img/1.jpg
你上傳前要把這個web相對路徑轉化成物理的絕對路徑再上傳
當然資料庫保存的是這個相對路徑/upload/img/1.jpg

❺ c#.net 圖片的上傳和保存

string filename = Path.GetFileName(FileUpload1.PostedFile.FileName).ToString();
//制定根路徑
string path = Server.MapPath("~/img/");
//利用時間的無重復性對圖片命名
string rename = DateTime.Now.ToString("yyyyMMddHHmm_");
//判斷是否存在此文件
if (FileUpload1.HasFile)
{
//獲取擴展名
string str = Path.GetExtension(FileUpload1.PostedFile.FileName.ToLowerInvariant());
if (str == ".bmp" || str == ".png" || str == ".jpeg" || str == ".gif" || str == ".jpg")
{
if (FileUpload1.PostedFile.ContentLength >= 120000)
{
Response.Write("<script>alert('上傳文件不能大於120kb');history.go(-1)</script>");
}
else{
//判斷是否存在此路徑
if (Directory.Exists(path))
{
FileUpload1.PostedFile.SaveAs(path + rename+filename);
}
else {
Directory.CreateDirectory(path);
FileUpload1.PostedFile.SaveAs(path +rename+ filename);
}
}

}
else{
Response.Write("<script>alert('文件上傳格式不正確');history.go(-1)</script>");
}
}
else {
Response.Write("<script>alert('請選擇上傳文件');history.go(-1)</script>");
}

這個地方只是對圖片進行上傳,如果要保存到資料庫中只需要獲取到你圖片上傳後,圖片的名稱,然後保存到資料庫。

❻ c#做.net網站,怎麼將圖片上傳到伺服器指定文件夾下。急請大家回答。

你保存的時候 指定了物理路徑 這點肯定不行 需使用Server.MapPath('文件路徑') 採用相對路徑存儲

❼ asp.net 圖片上傳方面的問題

protected void Button1_Click(object sender, EventArgs e)
{
string picName = Path.GetFileName(this.FileUpload1.PostedFile.FileName);
if (string.IsNullOrEmpty(picName))
{
Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('請選擇圖片文件')</script>");
return;
}

if (this.FileUpload1.HasFile)
{
int width = 200;
int height = 200;
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(this.FileUpload1.PostedFile.FileName);
if (imgPhoto.Width > width || imgPhoto.Height > height)
{
Session["imgName"] = "";
divImgShow.InnerHtml = "";
Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('圖片尺寸必須小於或等於" + width + "px*" + height + "px')</script>");
return;
}

string imgName = Guid.NewGuid().ToString() + picName.Substring(picName.LastIndexOf('.'), picName.Length - picName.LastIndexOf('.'));
string path = "~/imageLook/" + imgName;
Session["imgName"] = imgName;
this.FileUpload1.SaveAs(Server.MapPath("~/img/")+FileUpload1.FileName);
divImgShow.InnerHtml = "<img src='img/" + FileUpload1.FileName + "' alt=''/>";
}
else
{
Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('圖片格式不正確,應為.jpg|.gif格式')</script>");
return;
}

}
頁面
<form id="form1" runat="server">
<div>

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

</div>
<div id="divImgShow" runat="server"></div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>

❽ .NET如何使用OLEDB執行存儲過程對BLOB欄位進行增加、修改操作

可以將二進制大對象
(BLOB)
作為二進制或字元數據寫入資料庫,具體視數據源的欄位類型而定。若要將
BLOB
值寫入資料庫,請發出相應的
INSERT

UPDATE
語句並將
BLOB
值作為輸入參數傳遞(請參見將存儲過程用於命令)。如果
BLOB
存儲為文本格式(如
SQL
Server
text
欄位),則可將
BLOB
作為字元串參數傳遞。如果
BLOB
存儲為二進制格式(如
SQL
Server
image
欄位),則可將類型
byte
的數組作為二進制參數傳遞。
注意
BLOB
可能相當大,因此在作為單個值寫入時可能要使用大量的系統內存,從而降低應用程序的性能。若要減少寫入
BLOB
值時所使用的內存量,可以按「塊」將
BLOB
寫入資料庫。用該方法將
BLOB
寫入資料庫的過程具體取決於數據源的功能。有關按塊將
BLOB
值寫入
SQL
Server
的示例,請參見將
BLOB
值寫入
SQL
Server
時節省資源。
以下代碼示例將員工信息添加到
Northwind
資料庫的
Employees
表中。員工照片將從文件中讀取並添加到表中的
Photo
欄位,該欄位為
image
欄位。
public
static
void
AddEmployee(
string
lastName,
string
firstName,
string
title,
DateTime
hireDate,
int
reportsTo,
string
photoFilePath,
string
connectionString){
byte[]
photo
=
GetPhoto(photoFilePath);
using
(SqlConnection
connection
=
new
SqlConnection(
connectionString))
SqlCommand
command
=
new
SqlCommand(
"INSERT
INTO
Employees
(LastName,
FirstName,
"
+
"Title,
HireDate,
ReportsTo,
Photo)
"
+
"Values(@LastName,
@FirstName,
@Title,
"
+
"@HireDate,
@ReportsTo,
@Photo)",
connection);
command.Parameters.Add("@LastName",
SqlDbType.NVarChar,
20).Value
=
lastName;
command.Parameters.Add("@FirstName",
SqlDbType.NVarChar,
10).Value
=
firstName;
command.Parameters.Add("@Title",
SqlDbType.NVarChar,
30).Value
=
title;
command.Parameters.Add("@HireDate",
SqlDbType.DateTime).Value
=
hireDate;
command.Parameters.Add("@ReportsTo",
SqlDbType.Int).Value
=
reportsTo;
command.Parameters.Add("@Photo",
SqlDbType.Image,
photo.Length).Value
=
photo;
connection.Open();
command.ExecuteNonQuery();
}}public
static
byte[]
GetPhoto(string
filePath){
FileStream
stream
=
new
FileStream(
filePath,
FileMode.Open,
FileAccess.Read);
BinaryReader
reader
=
new
BinaryReader(stream);
byte[]
photo
=
reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
return
photo;}

❾ asp.net 上傳圖片,我的做法是先上傳到伺服器的文件夾images,然後把路徑輸入到資料庫

你最好把上傳的代碼跟數據綁定的代碼貼出來看看。最好的提議是你綁定數據的時候監控一下你所拿到的photoname這個欄位的值,很有可能這個欄位的值跟你的項目的目錄不符,所以才造成圖片無法顯示

❿ C#/.net怎麼把上傳的圖片保存在指定路徑

獲取原圖文件流
using (Image img = Image.FromStream(file.InputStream)) //file為傳入的圖片文件
{
// 保存原圖
img.Save(HttpContext.Current.Request.MapPath( 」保存的路徑「 + "/" + 「保存的名稱」));
}