1 / 15

ASP [#13]

ASP [#13]. ส่งเมล์ด้วย ASP การ Uploads ไฟล์ผ่าน Browser การสร้างระบบ User Online การทำปุ่ม Print แบบรูปภาพ ปุ่ม Next และ Back. ส่งเมล์ด้วย ASP. การส่งเมล์ด้วย asp ต้องอาศัย Component เพื่อช่วยในการทำงาน สามารถ downloads component ในหัวข้อ component ตัวอย่าง code การส่งเมล์

ezhno
Télécharger la présentation

ASP [#13]

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP [#13] ส่งเมล์ด้วย ASP การ Uploads ไฟล์ผ่าน Browser การสร้างระบบ User Online การทำปุ่ม Print แบบรูปภาพ ปุ่ม Next และ Back

  2. ส่งเมล์ด้วย ASP • การส่งเมล์ด้วย asp ต้องอาศัย Component เพื่อช่วยในการทำงาน สามารถ downloads component ในหัวข้อ component • ตัวอย่าง code การส่งเมล์ • <%Set SMTPObj = CreateObject(“NU.SMTP") ' เป็น Component ที่ใช้ส่งเมล์ SMTPObj.SMTPServer = "Mail.nu.ac.th" ' mail serverSMTPObj.ToAdd = "exam@domain.com"SMTPObj.FromAdd = "Webmaster@nu.ac.th"SMTPObj.Subject = "ทดสอบการส่งเมล์"SMTPObj.Body = "ได้รับแล้วตอบเมล์ด้วยเด้อ"SMTPObj.Connectresult = SMTPObj.EMailResponse.write "Sent " & resultSMTPObj.Quit%>

  3. การ Uploads ไฟล์ผ่าน Browser • uploads ไฟล์ขึ้นไปบน Serverเก็บใว้ในที่ที่ต้องการ • หลักการสามารถ Uplodas ไฟล์รูปภาพหรือไฟล์อื่นๆได้ขึ้นอยู่แต่ว่าจะกำหนดให้สามารถ Uploads ไฟล์ Format ใดบ้าง • ตัวอย่างที่จะทำต่อไปนี้เป็นเพียงการ uploadsรูปภาพลง Folder เท่านั้น • ส่วนประกอบของไฟล์- Sample1.asp ( Page สำหรับเลือกไฟล์ที่จะ uploads )- Sample2.asp ( page สำหรับทำการ uploads )- uploads.asp ( เป็นคำสั่งในการเปลี่ยนข้อมูลให้อยู่ในรูป Binary เพื่อใช้แทน Component สำหรับ uploads )

  4. ขั้นที่ 1สร้าง Folder picture เพื่อใว้เก็บไฟล์ต่างๆ • ขั้นที่ 2ให้ Copy Code ต่อไปนี้แล้ว Save เป็น uploads.asp

  5. Uploads.asp • <%Function TextToBinary(text)For i=1 to Len(text)character=Mid(text,i,1)TextToBinary=TextToBinary & ChrB(Asc(character))NextEnd FunctionFunction BinaryToText(Binary)BinaryToText=""For i=1 to LenB(Binary)character=MidB(Binary,i,1)BinaryToText=BinaryToText & Chr(AscB(character))NextEnd FunctionSet uploaddata=CreateObject("Scripting.Dictionary")data=Request.BinaryRead(Request.TotalBytes)posend=InStrB(1,data,TextToBinary(Chr(13)))header=MidB(data,1,posend-1)endheader=header&TextToBinary("_")pos_header=1pos_endheader=InStrB(1,data,endheader)Do While pos_header<>pos_endheaderSet sub_uploaddata=CreateObject("Scripting.Dictionary")pos_name=InStrB(pos_header,data,TextToBinary("name="))pos_namebegin=pos_name+6

  6. Uploads.asp (ต่อ) • pos_nameend=InStrB(pos_namebegin,data,TextToBinary(Chr(34)))name=BinaryToText(MidB(data,pos_namebegin,pos_nameend-pos_namebegin))pos_file=InStrB(pos_nameend,data,TextToBinary("filename="))enddata=InStrB(pos_nameend,data,header)If (pos_file<>0)And(pos_file<enddata) Thenpos_filebegin=pos_file+10pos_fileend=InStrB(pos_filebegin,data,TextToBinary(Chr(34)))filename=BinaryToText(MidB(data,pos_filebegin,pos_fileend-pos_filebegin))sub_uploaddata.Add "filename",filenamepos_content=InStrB(pos_fileend,data,TextToBinary("Content-Type:"))pos_contentbegin=pos_content+14pos_contentend=InStrB(pos_contentbegin,data,TextToBinary(Chr(13)))contenttype=BinaryToText(MidB(data,pos_contentbegin,pos_contentend-pos_contentbegin))sub_uploaddata.Add "contenttype",contenttypepos_valuebegin=pos_contentend+4pos_valueend=InStrB(pos_valuebegin,data,header)-2Value=BinaryToText(MidB(data,pos_valuebegin,pos_valueend-pos_valuebegin))sub_uploaddata.Add "value",valueElsepos_valuebegin=pos_nameend+4pos_valueend=InStrB(pos_valuebegin,data,header)-2Value=BinaryToText(MidB(data,pos_valuebegin,pos_valueend-pos_valuebegin))sub_uploaddata.Add "value",valueEnd Ifuploaddata.Add name,sub_uploaddatapos_header=InStrB(pos_header+LenB(header),data,header)Loop%>

  7. Sample1.asp • <html><body bgcolor="#FFFFFF"><form name="form1" method="post" action="Sample2.asp"enctype="multipart/form-data"><input type="file" name="file"><input type="submit" name="Submit" value="Uploads"></form></body></html>

  8. Sample2.asp • <html><body bgcolor="#FFFFFF"><%bytecount=Request.TotalBytes If bytecount<80000 Then ' กำหนดขนาดไฟล์ไม่เกิน 80 K%><!--#Include File="uploads.asp"--><% filename=uploaddata.Item("file").Item("filename")pos_filebegin=InStrRev(filename,"\")pos_fileend=Len(filename)filename_new=Mid(filename,pos_filebegin+1,pos_fileend-pos_filebegin) • ' กำหนดประเภทของไฟล์ที่จะสามารถ uploads ได้ • If Instr(filename_new,".jpg")<>0 Or Instr(filename_new,".gif")<>0 Or Instr(filename_new,".jpeg")<>0 Then • Set fs=Server.CreateObject("Scripting.FileSystemObject")Set uploadfile=fs.CreateTextFile(Server.Mappath("picture"&"/"&filename_new))uploadfile.Write uploaddata.Item("file").Item("value") • Response.write(" Uploads ไฟล์สำเร็จ ")Response.write("<br> ไฟล์ชื่อ " &filename_new)

  9. Sample2.asp (ต่อ) ' หากต้องการนำชื่อไฟล์ไปเก็บใว้ในฐานข้อมูลให้ทำการ Add ชื่อไฟล์ filename_new ลงในฐานข้อมูลจากนั้นเมื่อต้องการนำรูปภาพมาแสดง' ให้อ้างอิง Path Picture/filename_new uploadfile.Closeset fs=Nothing Else%>กรุณาเลือกไฟล์นามสกุล .gif , .jpg , หรือ jpeg เท่านั้น<br>กลับไปแก้ไข <a href="javascript:history.back()">คลิกที่นี่</a> <%End IfElse%>ขนาดไฟล์เกิน 80 KB กรุณาเลือกไฟล์ใหม่<br><br>กลับไปแก้ไข <a href="javascript:history.back()">คลิกที่นี่</a> <%End If%></body></html>

  10. นอกจากนี้ยังสามารถประยุกต์ใช้กับฐานข้อมูล หลักการก็ไม่ยากให้เก็บชื่อไฟล์ลงในฐานข้อมูล ซึ่งเรียนรู้คำสั่งการเพิ่มข้อมูลได้จากหัวข้อที่ผ่านมา • เมื่อต้องการนำรูปภาพมาแสดงก็แค่อ้าง folder/ชื่อไฟล์ก็จะได้รูปภาพที่ต้องการออกมา

  11. การแสดงข้อมูลจาก list menu Sam10-2.asp • <html><body bgcolor="#FFFFFF"><%Set Connect_DB=Server.CreateObject("ADODB.Connection")Connect_DB.Open "Driver=Microsoft Access Driver (*.mdb);DBQ="& Server.MapPath("db/mydatabase.mdb")sql="Select * from month"Set Rs=Connect_DB.execute(sql)%><form name="form1" method="post" action="Sample2.asp">เลือกเดือน<select name="select"><option>กรุณาเลือกเดือน</option><%while Not Rs.EOF%><option value="<% =Rs("id")%>"><% =Rs("month")%></option><%Rs.MoveNextwend%></select><input type="submit" name="Submit" value="ตกลง"></form> </body></html>

  12. การทำปุ่ม Print แบบรูปภาพ • รูปแบบ • <input type="image" src="รูปภาพ" onClick="window.print()"> • Sample • <input type="image" src="images/print.gif" onClick="window.print()"> • หรือ • <input type="submit" name="Submit" value="Print" onClick="window.print()"> Print

  13. ปุ่ม Next และ Back • Next • <input type="submit" name="Submit" value="Next > >" onClick="jascript:history.go(+1)"> • Back • <input type="submit" name="Submit" value="< < Back"onClick="jascript:history.go(-1)"> Next>> <<Back

  14. แบบ Link • <a href="javascript:history.back()"><< Back</a> ปุ่ม Close • <a href="javascript:window.close()">ปิดหน้าต่างนี้ </a>

  15. แบบฝึกหัด • -ให้เขียนการ Login เข้าระบบ กำหนดตัวแปลขึ้นมาเอง • เมื่อเข้าได้แล้วให้จำลองเมนู • และแสดงจำนวน User online • โดยต้อง Upload และแสดงภาพได้จาก Database • Database ประกอบด้วย ID, Name , Picfile User :: Password :: Id Name :: Somchai Name Submit เพิ่มรูป Browse Login.asp Submit Upload_img.asp Hi Sanya... Add Person Name Picture Show Images Somchai Somyong User 2 Onlines Menu.asp ShowImage.asp

More Related