[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

how ASP Upload File works ??? Urgent !!!!!!!!!

I have a presentation in the day after tomorrow. Because this is my final presentation before graduating, you know, it's very important. I know all the code written in asp except this code, i feel a litle confuse when some one ask me " how it works". Can anyone tell me how it works line by line ??. I appreciate all your help. This time, i can't do anymore. Please help me. It's urgent.
 Here : I have two file. The html page have a form, in which its has:
<input name="anh" type="text" id="txtHINH0" size="60" class="form1"><a href="javascript:openWindow2('admin_upload2.asp?di r=luutru&win=pop&targetis=anh')">[Upload]</a>
When we want to upload a file, we click on [Upload], a popup Windows will show, we click on the button "browse.." to open a window. we use this window to take the file we want to upload, then click  "open", then click "upload". After upload complete, we will see:


(http://i67.photobucket.com/albums/h294/tin3k48a/upload1.jpg)

We click on this link, this will file name will be display on this text box, after that, all info will be inserted in to Database.
 OK! here is admin_upload1.asp page:

<%
Dim qStrDir
Dim qStrWin
Dim targetis
qStrDir=Request.QueryString("dir")
qStrWin=Request.QueryString("win")
targetis=Request.QueryString("targetis")
%>
<html>
<head>
<title>Upload d&#7919; li&#7879;u</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
<!--
function insertStr(strValue,anh){
     window.opener.document.form1[anh].value=strValue;
}

function getFileExtension(filePath) { //v1.0
  fileName = ((filePath.indexOf('/') > -1) ? filePath.substring(filePath.lastIndexOf('/')+1,filePath.length) : filePath.substring(filePath.lastIndexOf('\\')+1,filePath.length));
  return fileName.substring(fileName.lastIndexOf('.')+1,fileName.length);
}

function checkFileUpload(form,extensions) { //v1.0
  document.MM_returnValue = true;
  if (extensions && extensions != '') {
    for (var i = 0; i<form.elements.length; i++) {
      field = form.elements[i];
      if (field.type.toUpperCase() != 'FILE') continue;
      if (field.value == '') {
        alert('File is required!');
        document.MM_returnValue = false;field.focus();break;
      }
      if (extensions.toUpperCase().indexOf(getFileExtension(field.value).toUpperCase()) == -1) {
        alert('This file is not allowed for uploading!');
        document.MM_returnValue = false;field.focus();break;
  } } }
}
//-->
</script>
</HEAD>
<Body>

<%
Sub BuildUploadRequest(RequestBin)

  PosBeg = 1
  PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
  if PosEnd = 0 then
    Response.Write "<b>Form was submitted with no ENCTYPE=""multipart/form-data""</b><br>"
    Response.Write "Please correct the form attributes and try again."
    Response.End
  end if
  boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
  boundaryPos = InstrB(1,RequestBin,boundary)

  Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))

    Dim UploadControl
    Set UploadControl = CreateObject("Scripting.Dictionary")

    Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
    Pos = InstrB(Pos,RequestBin,getByteString("name="))
    PosBeg = Pos+6
    PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
    Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
    PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
    PosBound = InstrB(PosEnd,RequestBin,boundary)

    If  PosFile<>0 AND (PosFile<PosBound) Then

      PosBeg = PosFile + 10
      PosEnd =  InstrB(PosBeg,RequestBin,getByteString(chr(34)))
      FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
      FileName = Mid(FileName,InStrRev(FileName,"\")+1)

      UploadControl.Add "FileName", FileName
      Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
      PosBeg = Pos+14
      PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))

      ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
      UploadControl.Add "ContentType",ContentType

      PosBeg = PosEnd+4
      PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
      Value = FileName
      ValueBeg = PosBeg-1
      ValueLen = PosEnd-Posbeg
    Else

      Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
      PosBeg = Pos+4
      PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
      Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
      ValueBeg = 0
      ValueEnd = 0
    End If

    UploadControl.Add "Value" , Value    
    UploadControl.Add "ValueBeg" , ValueBeg
    UploadControl.Add "ValueLen" , ValueLen    

    UploadRequest.Add name, UploadControl    

    BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
  Loop
End Sub


Function getByteString(StringStr)
  For i = 1 to Len(StringStr)
        char = Mid(StringStr,i,1)
       getByteString = getByteString & chrB(AscB(char))
  Next
End Function

Function getString(StringBin)
  getString =""
  For intCount = 1 to LenB(StringBin)
       getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
  Next
End Function

Function UploadFormRequest(name)
  on error resume next
  if UploadRequest.Item(name) then
    UploadFormRequest = UploadRequest.Item(name).Item("Value")
  end if  
End Function

UploadQueryString = Replace(Request.QueryString,"AF_upload=true","")
if mid(UploadQueryString,1,1) = "&" then
     UploadQueryString = Mid(UploadQueryString,2)
end if

AF_uploadAction = CStr(Request.ServerVariables("URL")) & "?AF_upload=true"
If (Request.QueryString <> "") Then  
  if UploadQueryString <> "" then
       AF_uploadAction = AF_uploadAction & "&" & UploadQueryString
  end if
End If

If (CStr(Request.QueryString("AF_upload")) <> "") Then
  AF_redirectPage = "success.asp"
  If (AF_redirectPage = "") Then
    AF_redirectPage = CStr(Request.ServerVariables("URL"))
  end if
   
  RequestBin = Request.BinaryRead(Request.TotalBytes)
  Dim UploadRequest
  Set UploadRequest = CreateObject("Scripting.Dictionary")  
  BuildUploadRequest RequestBin
 
  AF_keys = UploadRequest.Keys
  for AF_i = 0 to UploadRequest.Count - 1
    AF_curKey = AF_keys(AF_i)

    if UploadRequest.Item(AF_curKey).Item("FileName") <> "" then
      AF_value = UploadRequest.Item(AF_curKey).Item("Value")
      AF_valueBeg = UploadRequest.Item(AF_curKey).Item("ValueBeg")
      AF_valueLen = UploadRequest.Item(AF_curKey).Item("ValueLen")

      if AF_valueLen = 0 then
        Response.Write "<p><B>&#272;&#259; có m&#7897;t l&#7895;i x&#7843;y ra trong quá tr&#769;nh b&#7841;n upload file!</B><br><br>"
        Response.Write "Tên File: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
        Response.Write "File không t&#7891;n t&#7841;i ho&#7863;c r&#7895;ng.<br>"
        Response.Write "B&#7841;n vui l&#803;ng ki&#7875;m tra và <A HREF=""javascript:history.back(1)"">th&#7917; l&#7841;i</a>"
              response.End
         end if
     
      Dim AF_strm1, AF_strm2
      Set AF_strm1 = Server.CreateObject("ADODB.Stream")
      Set AF_strm2 = Server.CreateObject("ADODB.Stream")
     
      AF_strm1.Open
      AF_strm1.Type = 1 'Binary
      AF_strm2.Open
      AF_strm2.Type = 1 'Binary
       
      AF_strm1.Write RequestBin
      AF_strm1.Position = AF_ValueBeg
      AF_strm1.CopyTo AF_strm2,AF_ValueLen
   
      AF_curPath ="../assets/ebooks/"
      fname=UploadRequest.Item(AF_curKey).Item("FileName")
      on error resume next
      AF_strm2.SaveToFile Trim(Server.mappath(AF_curPath))& "\" & UploadRequest.Item(AF_curKey).Item("FileName"),2
      if err then
        Response.Write "<p><B>&#272;&#259; có m&#7897;t l&#7895;i x&#7843;y ra trong quá tr&#769;nh b&#7841;n upload file!</B><br><br>"
        Response.Write "Tên File: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
        Response.Write "Th&#432; m&#7909;c &#273;&#7875; upload file vào không t&#7891;n t&#7841;i ho&#7863;c không &#273;&#432;&#7907;c phép upload file vào &#273;ó.<br>"
        Response.Write "B&#7841;n vui l&#803;ng ki&#7875;m tra và <A HREF=""javascript:history.back(1)"">th&#7917; l&#7841;i</a>"
              err.clear
              response.End
         end if
    end if
  next
 

  If (AF_redirectPage <> "") Then
 %>
<table border="1" width="100%" cellspacing="0" cellpadding="2" style="border-collapse: collapse" bgcolor="#ECE9D8" bordercolorlight="#F4F2E8">
     <tr>
          <td>
          <table border="0" width="100%" cellspacing="1">
               <tr>
                    <td bgcolor="#00557D"><b>
                    <font face="Tahoma" size="2" color="#FFFFFF">&nbsp;Upload d&#7919; li&#7879;u</font></b></td>
               </tr>
               </table>
          </td>
     </tr>
     <tr>
          <td>
          <table border="0" width="100%" height="112">
               <tr>
                    <td height="71">
                    <p align="center" style="margin-top: 2px; margin-bottom: 2px">
                    <b><font face="Tahoma">Upload thành công!</font></b></p>
                    <p align="center" style="margin-top: 2px; margin-bottom: 2px">
                    <font face="Tahoma" size="2">B&#7845;m vào &#273;ây
                    <a href="Javascript:insertStr('<%=fname%>','<%=targetis%>');">
                    (<%=fname%>)</a></font></p>
                    <p align="center" style="margin-top: 2px; margin-bottom: 2px">
                    <font face="Tahoma" size="2">&#273;&#7875; &#273;&#432;a &#273;&#432;&#7901;ng d&#7851;n vào h&#7897;p nh&#7853;p
                hình &#7843;nh.</font></td>
               </tr>
               <tr>
                    <td>
                    <p align="center">
                                   <input onclick="javascript:history.back();" type="button" value="Continue" name="Continue">
                                   <input onclick="javascript:window.close();" type="button" value="Close" name="Close1"></td>
               </tr>
          </table>
          </td>
     </tr>
</table>
<%    
  end if  
 
Else
if UploadQueryString <> "" then
  UploadQueryString = UploadQueryString & "&AF_upload=true"
else  
  UploadQueryString = "AF_upload=true"
end if  
%>
<table border="1" width="100%" cellspacing="0" cellpadding="2" style="border-collapse: collapse" bgcolor="#ECE9D8" bordercolorlight="#F4F2E8">
     <tr>
          <td>
          <table border="0" width="100%" cellspacing="1">
               <tr>
                    <td bgcolor="#00557D"><b>
                    <font face="Tahoma" size="2" color="#FFFFFF">&nbsp;Upload</font></b></td>
               </tr>
               <tr>
               <form name="ASP" method="POST" enctype="multipart/form-data" action="admin_upload1.asp?dir=<%=qStrDir%>&targetis=<%=targetis%>&AF_upload=true">
                    <td>
                    <table border="0" width="100%">
                         <tr>
                              <td width="40%" height="10">                              
                              </td>
                              <td width="60%" height="10">                              
                              </td>
                         </tr>
                         <tr>
                              <td width="40%">                              
                              <p align="right"><font face="Tahoma" size="2">t&#234;n file:
                              </font></td>
                              <td width="60%">                              
                                   <input type="file" name="Files" size="25">
                              </td>
                         </tr>
                         <tr>
                              <td width="40%" height="5"></td>
                              <td width="60%" height="5">
                              </td>
                         </tr>
                         <tr>
                              <td width="40%">&nbsp;</td>
                              <td width="60%">
                                   <input type="submit" value="Upload" name="Submit1">
                                   <input onclick="javascript:window.close();" type="button" value="Close" name="Close">
                              </td>
                         </tr>
                         <tr>
                              <td width="40%" height="5"></td>
                              <td width="60%" height="5"></td>
                         </tr>
                    </table>
                    </td>
                </form>
               </tr>
          </table>
          </td>
     </tr>
</table>
<%End if%>
</Body>
</HTML>
0
hiepbeo
Asked:
hiepbeo
1 Solution
 
HuyBDCommented:
Hi hiepbeo
As you see,  pro use 4 object to implement is 2 Scripting.Dictionary  and 2 ADODB.Stream

Scripting.Dictionary like the list that stored key-value
BuildUploadRequest do that: UploadRequest stored all UploadControl, UploadControl stored all info from request (Request.BinaryRead(Request.TotalBytes))
then defined 2 stream
AF_strm1 to write all data from UploadControl if have "filename<>""" that specify file upload
AF_strm2 to copy from AF_strm1 get only content of file

then write to file.


0
 
hiepbeoAuthor Commented:
I have only 18 hours to absorb this code (including time to sleep and eat.)  Anyone can help me solve this problem, tell me more about this code ???
Thanks for all your help !!!!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now