Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2003
    Posts
    20

    Unanswered: Upload and rename

    I got a problem on passing request.form value to a binary read upload code.
    The first page is upload page, second page is pure ASP upload code.
    On first page, I put <input type="text" value="Mar" name="Mar">, coz I need to pass to second page for renaming the file, if the file name that I uploaded is hello.txt, then it will rename it to
    Mar-hello.txt ,but I don't know how to put the rename code in the coding, if anyone could help?

    The first page code is:

    <form method="post" name="uform" action="UploadFile-fso.asp" enctype="multipart/form-data">

    Attach report : <input type="file" name="f1" size="40"><BR>
    <BR>

    <input type="button" value="Attach!" onclick="fnValidate()">


    <input type="text" value="Mar" name="Mar">
    </form>



    The upload code on second page are as follows:

    <html><head><title>Attach Report</title></head>

    <body bgcolor="lightblue">

    <%@Language=vbscript%>

    <% 'option explicit
    Response.Expires = 0
    %>
    <%



    dim strspliter


    Public Function ConToByte(Byval NormString)
    Dim i
    For i = 1 to Len(NormString)
    ConToByte = ConToByte & ChrB(AscB(Mid(NormString,i,1)))
    Next
    End Function



    Public Function ConToString(byval ByteString)
    dim i
    For i = 1 to Lenb(ByteString)
    ConToString = ConToString & Chr(AscB(Midb(ByteString,i,1)))
    Next
    END Function


    Public Function OmitBothRL(ByVal strstring)
    dim intSpliterPlace
    intSpliterPlace = Instr(1,strstring,chr(13))
    strspliter = Left(strstring,intspliterPlace)

    OmitBothRL = mid(strstring,intSpliterPlace + 2,Len(strstring) - (2 * (Len(strspliter))+6) )
    End Function



    Function Spliter(ByVal strstring)
    Spliter = Split(strstring,strspliter)
    End Function



    Dim bolDispositionError
    Dim bolform_dataError
    Dim bolfilecontentError
    dim bolfilenameError


    Dim strstarter
    dim firstcotation
    dim secconcotation

    Function Recognize(ByVal strstring,ByVal Kind)
    If Kind = "filename" or Kind = "Content_Type" then
    Recognize = fileinfo(strstring,Kind)
    Else
    Recognize = Publicinfo(strstring,Kind)
    End if
    End Function



    Function publicinfo(ByVal strstring,ByVal Kind)
    Dim intDirectPlace
    Dim intSemiPlace
    Dim Result

    If Kind = "disposition" then
    If instr(1,strstring,"Content-Disposition") < 1 then
    bolDispositionError = True
    Else
    intSemiPlace = instr(1,strstring,chr(58))
    intDirectPlace = instr(1,strstring,chr(59))
    Result = Lcase(Trim(Mid(strstring,intSemiPlace + 1,(intDirectPlace -1) - intSemiPlace)))
    If Result = "form-data" then
    publicinfo = Result
    Else
    bolDispositionError = True
    End if
    End if


    Elseif Kind = "name" then
    If instr(1,strstring,"name") < 1 then
    bolform_dataError = True
    Else
    dim firstcotation
    dim secconcotation
    firstcotation = instr(1,strstring,chr(34))
    secconcotation = instr(firstcotation + 1,strstring,chr(34))

    publicinfo = Mid(strstring,firstcotation+1,(secconcotation-(firstcotation+1)))
    End if


    Elseif Kind = "filecontent" then

    strstarter = (instr(1,strstring,vbCrLf & vbCrLf)) + 4
    strstarter = trim(Mid(strstring,strstarter))
    If strstarter = vbCrLf then
    publicinfo = "EmptyFile"
    Else

    publicinfo = strstarter
    End if
    Else
    bolDispositionError = True
    bolform_dataError = True
    bolfilecontentError = True
    End if

    End Function



    Function fileinfo(ByVal strstring,ByVal Kind)
    If Kind = "filename" then
    if instr(1,strstring,"filename",1) < 1 then
    fileinfo = "notfile"
    ELse
    strstarter = Mid(strstring,(instr(1,strstring,"filename")),(ins tr(1,strstring,"Content-Type",1) - (instr(1,strstring,"filename"))))
    If (instr(1,strstring,"\")) < 1 then
    fileinfo = "EmptyField"
    Else
    firstcotation = cint(instr(1,strstarter,chr(34)) + 1)
    secconcotation = instr(firstcotation + 1,strstarter,chr(34))
    strstarter = Mid(strstarter,firstcotation,((secconcotation - firstcotation) ))
    fileinfo = Right(strstarter,(len(strstarter)-InstrRev(strstarter,"\")))
    End if
    End if
    Elseif Kind = "Content_Type" then
    if instr(1,strstring,"Content-Type",1) < 1 then
    fileinfo = "notfile"
    Else
    strstarter = Mid(strstring,Instr(1,strstring,"Content-Type",1)+14,(Instr(1,strstring,vbCrLf & vbCrLf,1) - Instr(1,strstring,"Content-Type",1)-14))
    fileinfo = strstarter
    End if
    End if
    End Function


    '___________________ No Function ___________________



    Public Sub UploadFile(ByVal UploadPath)

    Dim Strallstring
    strallstring = ConToString(Request.BinaryRead(Request.TotalBytes) )
    strallstring = OmitBothRL(strallstring)

    Dim strarray
    strarray = Spliter(strallstring)

    Dim i
    Dim DispositionArray,nameArray,filenameArray,Content_T ypeArray,filecontentarray

    dim obfso,obfile

    set obfso = server.CreateObject("scripting.FileSystemObject")
    dim FileCounter
    FileCounter = 0
    For i = 0 to UBound(strarray)

    DispositionArray = Recognize(strarray(i),"disposition")
    nameArray = Recognize(strarray(i),"name")
    filenameArray = Recognize(strarray(i),"filename")
    Content_TypeArray = Recognize(strarray(i),"Content_Type")
    filecontentarray = Recognize(strarray(i),"filecontent")'has no name.

    If (bolDispositionError=False) And (bolform_dataError=False) And (bolfilecontentError=False) And (bolfilenameError=False) then
    If Trim(filenameArray) = trim("EmptyField") then

    Elseif Trim(filenameArray) = trim("notfile") then

    Else

    Dim strherepath
    If Lcase(Trim(UploadPath)) = Lcase(Trim("here")) then
    strherepath = cstr(Request.ServerVariables("PATH_TRANSLATED"))
    strherepath = Left(strherepath,InstrRev(strherepath,"\"))
    Else
    If Lcase(trim(Right(UploadPath,1))) = "\" then
    strherepath = UploadPath
    Else
    strherepath = UploadPath & "\"
    End if
    End if

    if not(obfso.FolderExists(strherepath)) then
    Response.Write "The FolderThat You Want Upload File In That Is Not Exists"
    Response.End
    End if
    Set obfile = obfso.OpenTextFile(strherepath & filenameArray,2,True)
    obfile.write filecontentarray
    obfile.close
    FileCounter = FileCounter + 1
    Set obfile = Nothing

    End if
    Else
    Response.Write "An Error Occured"
    Response.End
    End if
    Next
    Response.Write FileCounter & " Files Uploaded Successfully."
    End Sub


    'if you inout "here" as sub(UploadFile) argument the file will upload near the .asp file.
    '.but you can input the physical path as argument to upoad file in your favorit place.


    Call UploadFile("here")




    %>

    <br><br>
    <input type="button" name="submit" value="close" onclick="window.close()">

    </body>

    </html>



    Thanks.
    Kai

  2. #2
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    in your code you have these lines...
    Code:
    obfile.write filecontentarray
    obfile.close
    FileCounter = FileCounter + 1
    Set obfile = Nothing
    you should be able to change it to something like,....
    Code:
    obfile.write filecontentarray
    obfile.close
    objfile.Name = "Mar-" & objFile.Name
    FileCounter = FileCounter + 1
    Set obfile = Nothing

  3. #3
    Join Date
    Feb 2003
    Posts
    20

    doesn't work

    Hi rockslide,

    it doesn't work, got error. And what I want is to pass the value from text box, not manually enter the value....

    Kai

  4. #4
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    dude,

    I realise that you want to use the value in the text box but I'm buggered if I am going to read through all that code to find out what variable you have stored that value in after you requested it from the form.

    As for the error, I don't suspose there is any chance that you want to post the error message and thus give me an opportunity to help you further.

  5. #5
    Join Date
    Feb 2003
    Posts
    20

    Error

    The error is :

    Error Type:
    Microsoft VBScript runtime (0x800A01A8)
    Object required: 'objfile'

    that line you wrote to me.

    Actually the textbox I pass it from other form.I just want to know how to write the code in the upload code.
    As you say about this line:
    objfile.Name = "Mar-" & objfile.Name

    So I assume that my line would be:
    objfile.Name = "request.form("textboxname")" & objfile.Name

    But request cannot be used in pure ASP code....
    Can you please run my code? the first page and second page you place in one folder, when you upload file, it will upload to the folder where you store these 2 files.

    Thanks

  6. #6
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    typo.....obfile not objfile...
    Code:
    obfile.write filecontentarray
    obfile.close
    obfile.Name = request.form("Mar") & obFile.Name
    FileCounter = FileCounter + 1
    Set obfile = Nothing
    now what the heck do you mean by pure asp the request object is a standard part of asp....

    I'll run your code if you attach the files rather then posting them directly into a message.

  7. #7
    Join Date
    Feb 2003
    Posts
    20

    error

    Error Type:
    Request object, ASP 0207 (0x80004005)
    Cannot use Request.Form collection after calling BinaryRead.

    got this error, I tried to use upload.form("Mar") or UploadRequestForm("Mar") also the same.

    if put upload.form("Mar") got the following error:
    Error Type:
    Microsoft VBScript runtime (0x800A01A8)
    Object required: 'upload'

    if put UploadRequestForm("Mar") got the following error:
    Error Type:
    Microsoft VBScript runtime (0x800A000D)
    Type mismatch: 'UploadRequestForm'

  8. #8
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    does it let you put a request.form here??
    Code:
    Dim textBoxValue
    textBoxValue = Request.Form("textBox")
    Dim Strallstring
    strallstring = ConToString(Request.BinaryRead(Request.TotalBytes) )
    strallstring = OmitBothRL(strallstring)
    then you can use
    Code:
    obfile.write filecontentarray
    obfile.close
    obfile.Name = textBoxValue & obFile.Name
    FileCounter = FileCounter + 1
    Set obfile = Nothing
    Edit: just checked msdn, this won't work either. I'll look into it
    Last edited by rokslide; 10-29-04 at 20:10.

  9. #9
    Join Date
    Feb 2003
    Posts
    20

    Still got error

    Error Type:
    Request object, ASP 0207 (0x80004005)
    Cannot use Request.Form collection after calling BinaryRead.

    at this line:
    textBoxValue = Request.Form("Mar")

  10. #10
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    yeah, you can't combine both request.form and request.binaryread.... have you tried doing it using a querystring instead??

  11. #11
    Join Date
    Feb 2003
    Posts
    20

    Errror

    When I put
    textBoxValue = Request.QueryString("Mar")

    it got error at this line:
    OmitBothRL = mid(strstring,intSpliterPlace + 2,Len(strstring) - (2 * (Len(strspliter))+6) )

    Error Type:
    Microsoft VBScript runtime (0x800A0005)
    Invalid procedure call or argument: 'Mid'

  12. #12
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    okie so it looks like request.querystring a request.binaryread will work together so you need to change your page so that when you do a submit you can the form method to get and in theory that should set things right.

    once again, if you attach the files so they are easy to download I'll test this out for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •