<%
Session.Codepage="65001"
If Session("user")="" Then
Response.Redirect("../login.asp")
End If
%>
<html>
<head>
<title>ภาควิชาอายุรศาสตร์ คณะแพทย์ศาสตร์ศิริราชพยาบาล </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function checkeng() {
var obj=frmMain.file1
var str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-." //กำหนดอักษรอังกฤษส่วนนี้ครับ
var val=obj.value
var valOK = true;
for (i=0; i<val.length & valOK; i++){
valOK = (str.indexOf(val.charAt(i))!= -1)
}
if (!valOK) {
alert("กรุณาอัพไฟล์ชื่อภาษาอังกฤษเท่านั้น")
obj.focus()
return false
} return true
if (document.frmMain.txtname.value == "")
{
alert('กรุณาใส่หัวข้อด้วย');
document.frmMain.txtname.focus();
return false;
}
if (document.frmMain.file1.value == "")
{
alert('กรุณาเลือกไฟล์ด้วย');
document.frmMain.file1.focus();
return false;
}
}
</script>
</head>
<body>
<form method="post" action="insertwork.asp" name="frmMain" enctype="multipart/form-data" onsubmit="return checkeng();">
<input type="hidden" name="username" value="<%=Session("user")%>">
<table align="center" border="0">
<tr>
<td>ชื่องาน</td>
<td><input type="text" name="txtname"></td>
</tr>
<tr>
<td>ตัวเอกสาร</td>
<td><input type="file" name="file1">***ชื่อไฟล์เอกสารต้องเป็นชื่อภาษาอังกฤษเท่านั้น</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="ตกลง"></td>
</tr>
</table>
</form>
</body>
</html>
อัพโหลด(ASP)
<!--#include file=upload.asp-->
<%
Session.Codepage="65001"
len1=Len(request("url"))-InstrRev(request("url"),"/")
pathinfo=Server.mappath(Request.ServerVariables("PATH_INFO"))
pathEnd = Len(pathinfo)-len1
filepath=left(pathinfo,pathEnd) & "work/"
Response.Expires=0
Response.Buffer = TRUE
Response.Clear
byteCount = Request.TotalBytes
RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")
BuildUploadRequest RequestBin
other_name = UploadRequest.Item("txtname").Item("Value")
'****************** file1 ******************
filepathname = UploadRequest.Item("file1").Item("FileName")
if filepathname <> "" then
filename = lcase(Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\")))
'response.write(filename & "<br>")
'response.write(len1 & "<br>")
'response.write(filepathname & "<br>")
'response.write("Other File" & other_name)
'response.write(filepath & "<br>")
value = UploadRequest.Item("file1").Item("Value")
'response.write(UploadRequest.Item("file1").Item("Value") & "<br>")
'response.write(value & "<br>")
limitSize = 200000 * 1024 ' 2000 kByte
if lenB(value) > limitSize then
response.write "<b>File ใหญ่เกิน " & _
formatNumber(limitSize / 1024) & _
" kB คลิก Back กลับไปเลือกใหม่"
response.end
end if
if Instr(filename,".doc")<>0 then
ty=".doc"
elseif Instr(filename,".xls")<>0 then
ty=".xls"
elseif Instr(filename,".ppt")<>0 then
ty=".ppt"
elseif Instr(filename,".pdf")<>0 then
ty=".pdf"
else
%>
<p align="center"><font color="#FF0000">อนุญาติให้ Upload เฉพาะ .doc, .xls, .ppt, .pdf, .zip, .rar, .swf เท่านั้น</font></p>
<p align="center"><a href="javascript:history.back();">กลับไปแก้ไข</a></p>
<%
response.end
end if
if lenB(value) = 0 then
%>
<p align="center"><font color="#FF0000">ไม่มีไฟล์นี้</font></p>
<p align="center"><a href="javascript:history.back();">กลับไปแก้ไข</a></p>
<%
response.end
end if
file1=filename
'-------------------- upload to server ------------------------
Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
Set fileObj = ScriptObject.CreateTextFile(filepath & file1)
For i = 1 to LenB(value)
fileObj.Write chr(AscB(MidB(value,i,1)))
Next
'Session("a")=file1
'fileObj.Close
dim con
set con=Server.CreateObject("ADODB.Connection")
con.open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=1234; Initial Catalog=med;Data Source=PANICHA-PC"
Dim sql,rec,sql1,rec1,d
sql1="select max(wid) as wid from work"
Set rec1=con.execute(sql1)
If Not rec1.EOF Then
Session("w")=rec1.fields("wid").value
End If
d=Date()
If Session("w")="" Then
sql="insert into work values(1,'" & other_name & "','" & file1 & "','" & d & "','" & Session("user") & "')"
Set rec=con.execute(sql)
response.redirect("work.asp")
Else
sql="insert into work values(" & Session("w") & ",'" & other_name & "','" & file1 & "','" & d & "','" & Session("user") & "')"
Set rec=con.execute(sql)
response.redirect("work.asp")
End If
end if
%>