asp上傳圖片資料庫
<%
Response.Buffer = True
Server.ScriptTimeOut=9999999
On Error Resume Next
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
<meta content="all" name="robots" />
<meta name="author" content="木目,Woodeye" />
<meta name="description" content="木目ASP文件上傳工具" />
<meta name="keywords" content="木目,ASP,Upload,文件上傳" />
<style type="text/css">
<!--
body,input {font-size:12px;}
-->
</style>
<title>木目ASP文件上傳工具</title>
</head>
<body id="body">
<%
ExtName = "jpg,gif,png,txt,rar,zip,doc" '允許擴展名
SavePath = "img" '保存路徑
If Right(SavePath,1)<>"/" Then SavePath=SavePath&"/" '在目錄後加(/)
CheckAndCreateFolder(SavePath)
UpLoadAll_a = Request.TotalBytes '取得客戶端全部內容
If(UpLoadAll_a>0) Then
Set UploadStream_c = Server.CreateObject("ADODB.Stream")
UploadStream_c.Type = 1
UploadStream_c.Open
UploadStream_c.Write Request.BinaryRead(UpLoadAll_a)
UploadStream_c.Position = 0
FormDataAll_d = UploadStream_c.Read
CrLf_e = chrB(13)&chrB(10)
FormStart_f = InStrB(FormDataAll_d,CrLf_e)
FormEnd_g = InStrB(FormStart_f+1,FormDataAll_d,CrLf_e)
Set FormStream_h = Server.Createobject("ADODB.Stream")
FormStream_h.Type = 1
FormStream_h.Open
UploadStream_c.Position = FormStart_f + 1
UploadStream_c.CopyTo FormStream_h,FormEnd_g-FormStart_f-3
FormStream_h.Position = 0
FormStream_h.Type = 2
FormStream_h.CharSet = "GB2312"
FormStreamText_i = FormStream_h.Readtext
FormStream_h.Close
FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"\")+1,FormEnd_g)
If(CheckFileExt(FileName_j,ExtName)) Then
SaveFile = Server.MapPath(SavePath & FileName_j)
If Err Then
Response.Write "文件上傳: <span style=""color:red;"">文件上傳出錯!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上傳文件</a><br />"
Err.Clear
Else
SaveFile = CheckFileExists(SaveFile)
k=Instrb(FormDataAll_d,CrLf_e&CrLf_e)+4
l=Instrb(k+1,FormDataAll_d,leftB(FormDataAll_d,FormStart_f-1))-k-2
FormStream_h.Type=1
FormStream_h.Open
UploadStream_c.Position=k-1
UploadStream_c.CopyTo FormStream_h,l
FormStream_h.SaveToFile SaveFile,2
SaveFileName = Mid(SaveFile,InstrRev(SaveFile,"\")+1)
Response.write "文件上傳: <span style=""color:red;"">" & SaveFileName & " </span>文件上傳成功! <a href=""" & Request.ServerVariables("URL") &""">繼續上傳文件</a><br />"
End If
Else
Response.write "文件上傳: <span style=""color:red;"">文件格式不正確!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上傳文件</a><br />"
End If
Else
%>
<script language="Javascript">
<!--
function ValidInput()
{
if(document.upform.upfile.value=="")
{
alert("請選擇上傳文件!")
document.upform.upfile.focus()
return false
}
return true
}
// -->
</script>
<form action='<%= Request.ServerVariables("URL") %>' method='post' name="upform" onsubmit="return ValidInput()" enctype="multipart/form-data">
文件上傳:
<input type='file' name='upfile' size="40"> <input type='submit' value="上傳">
</form>
<%
End if
Set FormStream_h = Nothing
UploadStream.Close
Set UploadStream = Nothing
%>
</body>
</html>
<%
'判斷文件類型是否合格
Function CheckFileExt(FileName,ExtName) '文件名,允許上傳文件類型
FileType = ExtName
FileType = Split(FileType,",")
For i = 0 To Ubound(FileType)
If LCase(Right(FileName,3)) = LCase(FileType(i)) then
CheckFileExt = True
Exit Function
Else
CheckFileExt = False
End if
Next
End Function
'檢查上傳文件夾是否存在,不存在則創建文件夾
Function CheckAndCreateFolder(FolderName)
fldr = Server.Mappath(FolderName)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fldr) Then
fso.CreateFolder(fldr)
End If
Set fso = Nothing
End Function
'檢查文件是否存在,重命名存在文件
Function CheckFileExists(FileName)
Set fso=Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(SaveFile) Then
i=1
msg=True
Do While msg
CheckFileExists = Replace(SaveFile,Right(SaveFile,4),"_" & i & Right(SaveFile,4))
If not fso.FileExists(CheckFileExists) Then
msg=False
End If
i=i+1
Loop
Else
CheckFileExists = FileName
End If
Set fso=Nothing
End Function
%>
2. asp中怎麼把圖片上傳到資料庫,然後再顯示出來
兩種方法,一種是把圖片以二進制流的形式存到資料庫,然後把流輸出到網頁上,這種方法很占資料庫資源,所以很少有人用。
二,把圖片上傳到空間,然後把圖片的路徑存到資料庫里。然後在網頁上讀到地址。(常用)
3. asp如何實現表單上傳圖片後存入access資料庫
asp中表單上傳圖片後會解析成二進制byte數組保存到access資料庫。
1、上傳圖片:
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
// image file path
textBox1.Text = open.FileName;
}
2、保存圖片信息到acess資料庫。
C#實現,保存核心代碼:
var pic = File.ReadAllBytes(yourFileName);
using(OleDbConnection con = new OleDbConnection(constr))
using(OleDbCommand cmd = new OleDbCommand("Insert Into DML_Books_List(ID, [Image]) values (@id, @image)", con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
cmd.Parameters.AddWithValue("@image", pic);
cmd.ExecuteNonQuery();
}
4. ASP如何實現批量上傳圖片,並在資料庫中有記錄
<%
dim conn
dim rs
dim rs2
set conn=server.createobject("adodb.connection")
conn.connectionstring="Provider = Microsoft.Jet.OLEDB.4.0;Data Source="&server.mapPath("db.mdb")
conn.open
formsize=request.totalbytes
if formsize<>0 then '這里只判斷了是否等於0,等於0就說明沒有傳過來數據。以後可以要多做點判斷,比如限制圖片大小的時候要判斷一下。
formdata=request.binaryread(formsize)
bncrlf=chrB(13)&chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
datastart=instrb(formdata,bncrlf&bncrlf)+4
dataend=instrb(datastart+1,formdata,divider)-datastart
mydata=midb(formdata,datastart,dataend)'這里也要檢測一下,就是檢測這個數據裡面是不是有病毒詞彙之類的,這個可以在網上查一下病毒裡面可能出現的詞語,如果有這些詞語,直接在這里就把這個mydata銷毀,然後response.redirect "xxx.asp",有病毒趕快跳轉,不要處理帶病毒的數據。
set rs=server.createobject("ADODB.recordset")
sql="select * from imgurl"
rs.open sql,conn,1,3
rs.addnew
rs("img").AppendChunk myData'把這個圖片的二進制數據新增到img欄位裡面。
tpm=replace(now(),"/","_") '用的日期做名字,日期裡面有"/"," ",":"之類的東西,這些東西全部替換成"_"。
tpm=replace(tpm,":","_")
tpm=replace(tpm," ","_")
rs("圖片名")=tpm
rs.update
Set MyStream=Server.CreateObject("Adodb.Stream")
MyStream.Type=1
MyStream.Open
MyStream.Write rs("img").getChunk(8000000)
MyStream.SaveToFile server.mappath("pic/"&tpm&".jpg")
MyStream.close
set MyStream=nothing
rs("img")="" '把access裡面的二進制圖片內容刪除,只保留圖片的名字。
rs.update
rs.close
set rs=nothing
response.write "<script>alert(""上傳成功"");location.href=""upload.asp"";</script>"
end if
%>
<form action="upload.asp" method="post" enctype="multipart/form-data">
<input type="file" name="imgurl">
<input type="submit" name="ok" value="上傳">
</form>
<!--------------下面是把圖片顯示出來------------------->
<div style="margin-top:2em;"><!--------------創建一個div把圖片顯示區域定位一下------------------->
<%
set rs2=server.createobject("ADODB.recordset")
sql2="select * from imgurl order by id desc"
rs2.open sql2,conn,1,1
for s=1 to rs2.recordcount
if not rs2.eof and not rs2.bof then
%>
<img src="pic/<%=rs2("圖片名")%>.jpg">
<%
else
exit for
end if
rs2.movenext
next
rs2.close
set rs2=nothing
%>
</div>
<%
conn.Close
Set conn = Nothing
%>
<!--至於刪除圖片,這個就很簡單了,圖片的名字都已經進access裡面了,直接讀一下access裡面的名字,讀了一個名字,然後用fso在pic文件夾裡面去刪除對應的圖片文件就可以了。--->
效果圖:網頁鏈接
5. 如何用ASP實現多處圖片上傳並保存到資料庫中
網頁上傳圖片多數不是把圖片保存在資料庫,是將圖片文件放在一個建好的目錄,上傳的時候,欄位里只保存圖片的路徑。
上傳多張圖片,用FOR循環
6. ASP.NET+C# FILEUPLOAD控制項,如何上傳圖片到伺服器並保存圖片路徑到資料庫
我做了個例子給你x0dx0a前台:x0dx0a