พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com | บล็อกอาหาร | Thailand travel information
ASP programming / แก้ไขรูปภาพ
aln
aln
Fri 8 Sep 2006 18:21:57

อยากให้พี่ช่วยดูโค้ดให้หน่อยครับว่าทำไมถึงแก้ไขรูปภาพไม่ได้ครับ

มันขึ้น Error ว่า

Error Type:
Microsoft VBScript runtime (0x800A01C9)
This key is already associated with an element of this collection
/Student_Affairs/rentart/adminshowart/include/upload.asp, line 37

**************************************************************

<%@ codepage="874" %>
<%  'if session ("login")<>"yes" then  response.redirect ("../indexart.asp")%>
<!--#include file="include/dbconnect.asp" -->
<!--#include file="include/validate.asp" -->
<!--#include file="include/upload.asp" -->
<%
'--------------------------------------------------------------
Response.Buffer = TRUE
'--------------------------------------------------------------
' 1.กำหนด folder ที่ต้องการเก็บรูปภาพสินค้า
filepath=Server.MapPath("../simage")
%>
<html>
<head><title>จัดการระบบเช่ายืมอุปกรณ์ศิลปวัฒธรรม</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
<link rel="stylesheet" type="text/css"  href="../../style/style1.css">
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="24%">&nbsp; </td>
    <td width="76%" valign="top">
<%
'--------------------------------------------------------------
' 2.รับค่าจากฟอร์มในหน้าที่แล้ว
byteCount = Request.TotalBytes
RequestBin = Request.BinaryRead(byteCount)
'--------------------------------------------------------------
' 3.ประกาศตัวแปรและเรียกใช้ Dictionary component
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")
BuildUploadRequest  RequestBin
artcatid = UploadRequest.Item("ArtCatID").Item("Value")
typeartid = UploadRequest.Item("TypeArtID").Item("Value")
artcatname = UploadRequest.Item("ArtCatName").Item("Value")
artdateadd = UploadRequest.Item("ArtDateAdd").Item("Value")
artdateend = UploadRequest.Item("ArtDateEnd").Item("Value")
place = UploadRequest.Item("Place").Item("Value")
artcatdetail = doEmpty(UploadRequest.Item("ArtCatDetail").Item("Value"))
artcatactive = doZero(UploadRequest.Item("ArtCatActive").Item("Value"))
' รับค่าไฟล์รูปเดิม
exartcatimage = UploadRequest.Item("exArtCatImage").Item("Value")
'------------------------------------------------------------------
' 4.รับค่าฟิลด์ประเภท blob
' 4.1.ภาพสินค้ารูปเล็ก
filepathname = UploadRequest.Item("ArtCatImage").Item("FileName")
artcatimage = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
psvalue = UploadRequest.Item("ArtCatImage").Item("Value")
%>
<%
' 3.แปลงค่าของคอนโทรล checkbox เพื่อจัดเก็บลงในฟิลด์ Yes/No
If artcatactive = "on" Then
 artcatactive = 1
Else
 artcatactive = 0
End If

'ตรวจสอบว่ากดปุ่ม Update หรือ Delete มาหรือไม่
If Trim(action) = "" Then
Response.Write "ไม่สามารถแก้ไข/ลบได้ กรุณาเลือกรายการสินค้าใหม่อีกครั้ง<br>"
Response.Write "<a href=edittypeshowart.asp>Back</a>"
Response.End
End If
%>
<%
'------------------------------------------------------------------
' 6.สร้าง sub program สำหรับทำขั้นตอนต่อไปนี้
' 6.1.validate ค่าฟิลด์ประเภท blob
' 6.2.จำกัดขนาดของไฟล์ฟิลด์ประเภท blob
' 6.3.upload to server
Sub uploadimage(imgvalue,imgname)
 ' 6.1.validate ค่าฟิลด์ประเภท blob
 if lenB(imgvalue) = 0 then
   response.write "<b>Can't not Upload " & "ไม่มีไฟล์นี้ / ไฟล์มีขนาดเป็นศูนย์   คลิก Back </b>"
   response.end
 end if
 
 ' 6.2.จำกัดขนาดของไฟล์ฟิลด์ประเภท blob
 limitSize =1000 * 1024   ' 200 kByte
 if lenB(imgvalue) > limitSize  then
    response.write "<b>File ใหญ่เกิน " & FormatNumber(limitSize / 1024) & " KB  คลิก Back กลับไปเลือกใหม่"
    response.end
 end if
 
 ' 6.3.upload to server
 Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
 Set fileObj = ScriptObject.CreateTextFile(filepath & "/" & imgname)
 For i = 1 to LenB(imgvalue)
   fileObj.Write chr(AscB(MidB(imgvalue,i,1)))
 Next
 fileObj.Close
End Sub
%>
<%
'-------------------------------------------------------------------
' 7.เรียกใช้ uploadimage()
If artcatimage <> "" Then
Call uploadimage(psvalue,artcatimage)
end if
%>
      <%
'-------------------------------------------------------------------
' 8.สร้าง sub program สำหรับลบไฟล์รูปภาพสินค้าเดิม
Sub deleteoldfile(imgfilename)
 Set fs = Server.CreateObject("Scripting.FileSystemObject")
 fs.DeleteFile filepath & "/" & imgfilename, True
End Sub
%>
 <%
'-------------------------------------------------------------------
' 9.ติดต่อฐานข้อมูลและบันทึกข้อมูลสินค้าลงในตาราง Products
' 9.1.กรณีกดปุ่ม Update
If action = "แก้ไข" Then
 If ConnectDB() = True Then
 ' กรณีใช้รูปภาพเดิมสำหรับรูปเล็กและรูปใหญ่
  If artcatimage = ""  Then
   sql = "update TblArtCategories " & _
     "set TypeArtID='"& typeartid &"'," & _
     "ArtCatName='"& artcatname &"'," & _
     "ArtCatImage='"& exartcatimage &"'," & _
     "ArtDateAdd='"& artdateadd &"'," & _
     "ArtDateEnd='"& artdateend &"'," & _
     "Place='"& place &"'," & _
     "ArtCatDetail='"& artcatdetail &"'," & _
     "ArtCatActive='"& artcatactive &"' " & _
     "where ArtCatID=" & artcatid
  ElseIf artcatimage <> ""Then
  ' กรณีใช้รูปภาพใหม่สำหรับรูปเล็กและรูปใหญ่
    sql = "update TblArtCategories " &_
     "set TypeArtID='"& typeartid &"'," & _
     "ArtCatName='"& artcatname &"'," & _
     "ArtCatImage='"& artcatimage &"'," & _
     "ArtDateAdd='"& artdateadd &"'," & _
     "ArtDateEnd='"& artdateend &"'," & _
     "Place='"& place &"'," & _
     "ArtCatDetail='"& artcatdetail &"'," & _
     "ArtCatActive='"& artcatactive &"' " & _
     "where ArtCatID=" & artcatid
  ' ลบไฟล์รูปภาพเดิมสำหรับรูปเล็กและรูปใหญ่
   Call deleteoldfile(exartcatimage)
       
  End If     
   Conn.Execute sql
   Response.Write "แก้ไขกิจกรรม <b>" & artcatname & "</b> เรียบร้อยแล้ว <br>ถ้าต้องการแก้ไขกิจกรรมเพิ่มให้ "
   Response.Write "<a href=edittypeshowart.asp>คลิกที่นี่</a>"
   Response.End
 End If
End If
'--------------------------------------------------------------
' 9.2.กรณีกดปุ่ม Delete
If action = "ลบ" Then
 If ConnectDB() = True Then
   sql = "delete from TblArtCategories " & _
     "where ArtCatID='" & artcatid &"'"
   ' ลบไฟล์รูปภาพเดิมสำหรับรูปเล็กและรูปใหญ่
   Call deleteoldfile(exartcatimage)
   Conn.Execute sql
   Response.Write "ลบกิจกรรม <b>" & artcatname & "</b> เรียบร้อยแล้ว <br>ถ้าต้องการลบกิจกรรมเพิ่มให้ "
   Response.Write "<a href=edittypeshowart.asp>คลิกที่นี่</a>"
   Response.End
 End If
End If
'--------------------------------------------------------------
%>

 </td>
  </tr>
</table>
</body>
</html>

Administrator
Fri 8 Sep 2006 19:39:15
ลองตรวจสอบการใช้งาน Dictionary ดูครับ Error แจ้งว่ามีการใช้งาน key ซ้ำกันครับ
Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login