Avatar billede no_doubt Nybegynder
13. august 2005 - 23:48 Der er 1 kommentar og
1 løsning

upload ren udgave

Jeg har fundet nedenstående script

Jeg ønsker et par ting hvis det kan laves.
1: at listen med alle filerne skal væk.
2: at der kun er input, browse og upload knapperne
3: en besked er fortæller hvilken fil jeg har uploaded
4: frasortering af diverse unødvendige ting i scriptet hvis dette findes.

vedr. pkt 4 synes jeg godt det kunne se sådan ud i bunden.

<%
    Dim UPLOAD_PATH
        UPLOAD_PATH    = Server.MapPath("./" & rs("mappe"))

    Dim g_oFso, g_oFolder, g_oFile
        Set g_oFso    = Server.CreateObject("Scripting.FileSystemObject")
        Set g_oFolder    = g_oFso.getFolder(UPLOAD_PATH)

    If (Request.ServerVariables("REQUEST_METHOD") = "POST") Then
        Dim g_oUpload
            Set g_oUpload    = get_upload_files()

        Dim fpos, fcontent
            fcontent    = g_oUpload("upload").Item("content")
        Set g_oFile    = g_oFso.CreateTextFile(UPLOAD_PATH & "\" & extract_filename(g_oUpload("upload").Item("filename")))
        For fpos = 1 to LenB(fcontent)
            g_oFile.Write chr(AscB(MidB(fcontent, fpos, 1)))
        Next
        g_oFile.Close: Set g_oFile    = Nothing

        Response.Redirect Request.ServerVariables("SCRIPT_NAME")
    End If

    Response.Write "<html><body><table>"
    Response.Write "<tr><td><hr></td></tr>"
    For Each g_oFile In g_oFolder.Files
        Response.Write "<tr><td><a href=""files/" & g_oFile.Name & """>" & g_oFile.name & "</a></td></tr>"
    Next
    Response.Write "<tr><td><hr></td></tr>"
    Response.Write "<tr><td align=""right""><form name=""frmUpload"" method=""post"" enctype=""multipart/form-data"" action=""" & Request.ServerVariables("SCRIPT_NAME") & """>"
    Response.Write "<input type=""file"" name=""upload""> &nbsp; <input type=""submit"" value=""upload""></td></tr>"
    Response.Write "</table></body></html>"

%><%
    Function get_upload_files()
        Dim    upload_object, request_binaries
        Dim position_start, position_end
        Dim boundary, boundary_pos

        Set upload_object    = Server.CreateObject("Scripting.Dictionary")
        request_binaries    = Request.BinaryRead(Request.TotalBytes)
        position_start        = 1
        position_end        = InstrB(position_start, request_binaries, get_byte_string(chr(13)))
        boundary            = MidB(request_binaries, position_start, (position_end - position_start))
        boundary_pos        = InstrB(1, request_binaries, boundary)

        Do Until (boundary_pos = InstrB(request_binaries, boundary & get_byte_string("--")))
            If Not(Response.IsClientConnected) Then Response.End

            Dim name, pos_file

            position_start    = (InstrB(InstrB(boundary_pos, request_binaries, get_byte_string("Content-Disposition")), request_binaries, get_byte_string("name=")) + 6)
            position_end    = InstrB(position_start, request_binaries, get_byte_string(chr(34)))

            name            = get_string(MidB(request_binaries, position_start, (position_end - position_start)))
            pos_file        = InstrB(boundary_pos, request_binaries, get_byte_string("filename="))

            If  ((pos_file <> 0) AND (pos_file < InstrB(position_end, request_binaries, boundary))) Then
                upload_object.Add name, Server.CreateObject("Scripting.Dictionary")

                position_start    = (pos_file + 10)
                position_end    = InstrB(position_start, request_binaries, get_byte_string(chr(34)))
                upload_object.item(name).Add "filename", get_string(MidB(request_binaries, position_start, (position_end - position_start)))

                position_start    = (InstrB(position_end, request_binaries, get_byte_string("Content-Type:")) + 14)
                position_end    = InstrB(position_start, request_binaries, get_byte_string(chr(13)))
                upload_object.item(name).Add "content-type", get_string(MidB(request_binaries, position_start, (position_end - position_start)))

                position_start    = (position_end + 4)
                position_end    = InstrB(position_start, request_binaries, boundary) - 2
                upload_object.item(name).Add "size", ((position_end - position_start))
                upload_object.item(name).Add "content", MidB(request_binaries, position_start, (position_end - position_start))
            End If
            boundary_pos    = InstrB(boundary_pos + LenB(boundary), request_binaries, boundary)
        Loop

        Set get_upload_files = upload_object
    End Function

    Function get_byte_string(str)
        Dim cnt
        For cnt = 1 to Len(str)
            get_byte_string    = get_byte_string & chrB(AscB(Mid(str, cnt, 1)))
        Next
    End Function

    Function get_string(str)
        Dim cnt
        For cnt = 1 to LenB(str)
            get_string    = get_string & chr(AscB(MidB(str, cnt, 1)))
        Next
    End Function

    Function extract_filename(filename)
        extract_filename    = Right(filename, Len(filename) - InStrRev(filename, "\", -1, 1))
    End Function
%>
Avatar billede busschou Praktikant
14. august 2005 - 07:42 #1
<%
Dim UPLOAD_PATH, g_oFso, g_oFolder, g_oFile, g_oUpload, fpos, fcontent

UPLOAD_PATH = Server.MapPath("./" & rs("mappe"))
Set g_oFso = Server.CreateObject("Scripting.FileSystemObject")
Set g_oFolder = g_oFso.getFolder(UPLOAD_PATH)

If (Request.ServerVariables("REQUEST_METHOD") = "POST") Then
  Set g_oUpload = get_upload_files()
  fcontent = g_oUpload("upload").Item("content")
  Set g_oFile = g_oFso.CreateTextFile(UPLOAD_PATH & "\" & extract_filename(g_oUpload("upload").Item("filename")))
  For fpos = 1 to LenB(fcontent)
    g_oFile.Write chr(AscB(MidB(fcontent, fpos, 1)))
  Next
  g_oFile.Close
  Set g_oFile = Nothing
  Session("upload") = g_oFile
  Response.Redirect Request.ServerVariables("SCRIPT_NAME")
End If

If session("upload") <> "" Then
  reponse.write "Du har uploadet hertil : "&session("upload")&"<br>"
End If
%>
<form name="frmUpload" method="post" enctype="multipart/form-data" action="<%=Request.ServerVariables("SCRIPT_NAME")%>">
  <input type="file" name="upload">&nbsp;<input type="submit" value="upload">
</form>
<%
Function get_upload_files()
  Dim upload_object, request_binaries, position_start, position_end, boundary, boundary_pos, name, pos_file
  Set upload_object = Server.CreateObject("Scripting.Dictionary")
  request_binaries = Request.BinaryRead(Request.TotalBytes)
  position_start = 1
  position_end = InstrB(position_start, request_binaries, get_byte_string(chr(13)))
  boundary = MidB(request_binaries, position_start, (position_end - position_start))
  boundary_pos = InstrB(1, request_binaries, boundary)

  Do Until (boundary_pos = InstrB(request_binaries, boundary & get_byte_string("--")))
    If Not(Response.IsClientConnected) Then
      Response.End
    End IF
    position_start = (InstrB(InstrB(boundary_pos, request_binaries, get_byte_string("Content-Disposition")), request_binaries, get_byte_string("name=")) + 6)
    position_end = InstrB(position_start, request_binaries, get_byte_string(chr(34)))
    name = get_string(MidB(request_binaries, position_start, (position_end - position_start)))
    pos_file = InstrB(boundary_pos, request_binaries, get_byte_string("filename="))
    If  ((pos_file <> 0) AND (pos_file < InstrB(position_end, request_binaries, boundary))) Then
      upload_object.Add name, Server.CreateObject("Scripting.Dictionary")
      position_start = (pos_file + 10)
      position_end = InstrB(position_start, request_binaries, get_byte_string(chr(34)))
      upload_object.item(name).Add "filename", get_string(MidB(request_binaries, position_start, (position_end - position_start)))
      position_start = (InstrB(position_end, request_binaries, get_byte_string("Content-Type:")) + 14)
      position_end = InstrB(position_start, request_binaries, get_byte_string(chr(13)))
      upload_object.item(name).Add "content-type", get_string(MidB(request_binaries, position_start, (position_end - position_start)))
      position_start = (position_end + 4)
      position_end = InstrB(position_start, request_binaries, boundary) - 2
      upload_object.item(name).Add "size", ((position_end - position_start))
      upload_object.item(name).Add "content", MidB(request_binaries, position_start, (position_end - position_start))
    End If
    boundary_pos = InstrB(boundary_pos + LenB(boundary), request_binaries, boundary)
  Loop
  Set get_upload_files = upload_object
End Function

Function get_byte_string(str)
  Dim cnt
  For cnt = 1 to Len(str)
    get_byte_string = get_byte_string & chrB(AscB(Mid(str, cnt, 1)))
  Next
End Function

Function get_string(str)
  Dim cnt
  For cnt = 1 to LenB(str)
    get_string = get_string & chr(AscB(MidB(str, cnt, 1)))
  Next
End Function

Function extract_filename(filename)
  extract_filename = Right(filename, Len(filename) - InStrRev(filename, "\", -1, 1))
End Function
%>
Avatar billede busschou Praktikant
14. august 2005 - 07:46 #2
i ovenstående i forhold til dine fire ønsker så
1)
listen over eksisterende filer er fjernet
2)
tabellen er fjernet så der kun er en form med et input felt
3)
Da der laves en redirect efter upload, så er det nødvendigt at gemme stien og filnavnet et sted, dette er gjort i en session, som så udskrives hvis den eksisterer
4)
Koden i bunden er funktioner som bruges for at kunne læse den ønskede fil til upload og kan derfor ikke fjernes med mindre de skrives direkte ind i den øverste kode, men det bliver koden ikke mere overskuelig af. Hvis du ønsker mere overskuelig kode bør du finde ud af om ikke dit webhotel understøtter en komponent til upload så man slipper for alt det her manuelle læsning af hvert tegn i filen

:o)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester