从数据库取出上传的图片,把该图片保存到服务器磁盘上,如何实现?

jslearner 2004-08-11 03:54:09
在我的一个aspx.cs上需要从数据库里取出很多图片,我需要把这些图片存到服务器磁盘上,然后将服务器上图片的路径返回回来,作为另外一张网页上img的 src的参数.
我已经能从数据库中得到
图片的编号(ImageID)和内容(ImageContent)了,我需要把这个ImageContent存到服务器上,并且将这个图片命名为ImageID.ico.
我这样写的,但是不正确
private void CreatIcon(DataTable dv)
{
string ImageID="wr";
string filePath=Http://" + HttpContext.Current.Request.Url.Host +HttpContext.Current.Request.ApplicationPath+"/images" ;
try
{
foreach (DataRow dr in dv.Rows)
{
ImageID = dr["ImageID"].ToString();
byte[] buffer = (byte[])dr["ImageContent"];
filePath = filePath+ @"\"+ImageID+".ico";
FileStream fout = new FileStream(pp,FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(fout);
bw.Write(buffer);
bw.Close();
fout.Close();
bw = null;
fout = null;
}
}
...全文
464 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jslearner 2004-08-12
  • 打赏
  • 举报
回复
对不起各位了,是我需要的那个图片的结果集里,没有返回ImageContent这列给我,不过谢谢大家的帮忙。
acewang(龍芯*Inside!) :用你的方法image.save,我存出来的图片比实际要小,所以不能正常显示,我不知道是怎么回事啊,我用的方法是
foreach(DataRow dr in ds.Tables ["dt"].Rows)
{
ImageID= dr["ImageID"].ToString();
byte[] buffer =(byte[])dr["ImageContent"];
FileStream fout = new FileStream(filePath,FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(fout);
bw.Write(buffer);
bw.Close();
fout.Close();
Response.BinaryWrite(buffer);
bw = null;
fout = null;
}
其实和 goodhy的方法差不多啊,只是我是用C#写的,谢谢各位了
goodhy 2004-08-11
  • 打赏
  • 举报
回复
给分!绝对和你胃口
从oracle中取图片,并显示
Dim conn As New OracleClient.OracleConnection()
Dim cmd As New OracleClient.OracleCommand()
Dim myReader As OracleClient.OracleDataReader
Dim sql As String
Dim fl As File

sql = "select jt from sd_gtzp" '从数据库中取出图片数据 blob
conn.ConnectionString = "Password=jlsbgis;User ID=jlsbgis;Data Source=sj"
cmd.CommandText = sql
cmd.Connection = conn

conn.Open()
myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
myReader.Read()

Dim fs As FileStream ' Writes the BLOB to a file (*.bmp).
Dim bw As BinaryWriter ' Streams the binary data to the FileStream object.
Dim bufferSize As Integer = 1000 ' The size of the BLOB buffer.
Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte() buffer to be filled by GetBytes.
Dim retval As Long ' The bytes returned from GetBytes.
Dim startIndex As Long = 0 ' The starting position in the BLOB output.
Dim fpath As String

fpath = Server.MapPath(Request.ApplicationPath) & "\Image\photo.bmp" '将图片数据存成本地文件
fs = New FileStream(fpath, FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)

' Reset the starting byte for a new BLOB.
startIndex = 0

' Read bytes into outbyte() and retain the number of bytes returned.
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize)

' Continue reading and writing while there are bytes beyond the size of the buffer.
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()

' Reposition the start index to the end of the last buffer and fill the buffer.
startIndex = startIndex + bufferSize
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize)
Loop

' Write the remaining buffer.
bw.Write(outbyte)
bw.Flush()

' Close the output file.
bw.Close()
fs.Close()

' Close the reader and the connection.
myReader.Close()
conn.Close()

Dim logo As Image '文件格式转换
logo = Image.FromFile(fpath)
fpath = Server.MapPath(Request.ApplicationPath) & "\Image\zkjhy.jpeg"
logo.Save(fpath, System.Drawing.Imaging.ImageFormat.Jpeg)

Me.Image1.Width = New Unit(300) '调整显示大小
Me.Image1.Height = New Unit(350)
Me.Image1.ImageUrl = "image/zkjhy.jpeg"
jslearner 2004-08-11
  • 打赏
  • 举报
回复
为什么同样的语句写在窗体程序里就不会错啊,如果是这个原因,我该怎样从数据集里的表来获得这个图片啊
jslearner 2004-08-11
  • 打赏
  • 举报
回复
byte[] buffer = (byte[])dr["ImageContent"];
这句话好象有问题,会出错说
异常详细信息: System.InvalidCastException: 指定的转换无效
jslearner 2004-08-11
  • 打赏
  • 举报
回复
还是不行啊,我数据库里有数据,有30张图片啊,但是不行啊,我要是想保存到现在访问的路径下该怎样啊?
acewang 2004-08-11
  • 打赏
  • 举报
回复
访问目录外的路经会没有权限,可以修改web.config:
<system.web>
<identity impersonate = "true" />
...
</system.web>
还有保证库里面确实有数据
jslearner 2004-08-11
  • 打赏
  • 举报
回复
怎么都不行啊?为什么我就保存不了,打开images文件夹是空的呀!
jslearner 2004-08-11
  • 打赏
  • 举报
回复
acewang(龍芯*Inside!) :我照你的方法做,但是images下没有看见这个图片啊?在aspx上能用("c:\\images\\.123.ico"这种路径吗?
acewang 2004-08-11
  • 打赏
  • 举报
回复
byte[] buffer = (byte[])dr["ImageContent"];
MemoryStream stream = new MemoryStream(buffer);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
img.Save("c:\\images\\.123.ico",System.Drawing.Imaging.ImageFormat.Icon);
山书生 2004-08-11
  • 打赏
  • 举报
回复
up
jslearner 2004-08-11
  • 打赏
  • 举报
回复
其实我要实现的功能很简单,我能根据图片的ImageID 生成图片内容为ImageContent的文件,并按他的ImageID来命名这个图片,然后图片的后缀名取为.ico,就行了,我只要能寸这个图片就好了
孟子E章 2004-08-11
  • 打赏
  • 举报
回复
作为另外一张网页上img的 src的参数?

http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1
jslearner 2004-08-11
  • 打赏
  • 举报
回复
我取filePath的方法对吗?
我知道在webservice上可以用FileStream这些方法来将数据库中的图片存到服务器硬盘上,但我不知道这样在aspx上可以吗?
jslearner 2004-08-11
  • 打赏
  • 举报
回复
FileStream fout = new FileStream(pp,FileMode.OpenOrCreate);
这句里的pp是写错了,应该是filePath

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧