Avatar billede bruhnsweb Nybegynder
15. april 2005 - 08:28 Der er 10 kommentarer og
1 løsning

Upload af filer

Hej Eksperter

Jeg skal gerne bruge et script som kan uploade: pdf, doc, tif, jpg osv.. umiddelbart ingen begrænsning på typen (på nær .exe).

Jeg har selv et uploadscript, men det kan kun uploade gif, og jpg..

Så er der nogen af jer der kender et upload script af filer, hvor man også kan slette dem igen fra serveren.

//Bruhn
Avatar billede busschou Praktikant
15. april 2005 - 08:31 #1
Hvordan ser dit script ud? I mange scripts findes der en liste over de godkendte filtyper. Hvis der gør det kan du jo blot tilføje dem du ønsker. Eller evt lave en liste over dem du ikke ønsker hvis det er nemmere
Avatar billede bruhnsweb Nybegynder
15. april 2005 - 08:35 #2
Inc-fil:
<%
' ------------------------------------------------------------------------------
' Container of Field Properties
Class clsField
    Public FileName
    Public ContentType
    Public Value
    Public FieldName
    Public Length
    Public BinaryData
End Class
' ------------------------------------------------------------------------------
Class clsUpload
' ------------------------------------------------------------------------------
    Private nFieldCount
    Private oFields()
 
' ------------------------------------------------------------------------------
    Public Property Get Count()
        Count = nFieldCount
    End Property
' ------------------------------------------------------------------------------
    Public Default Property Get Field(ByRef asFieldName)
        Dim lnLength
        Dim lnIndex
     
        lnLength = UBound(oFields)
     
        If IsNumeric(asFieldName) Then
            If lnLength >= asFieldName And asFieldName > -1 Then
                Set Field = oFields(asFieldName)
            Else
                Set Field = New clsField
            End If
        Else
            For lnIndex = 0 To lnLength
                If LCase(oFields(lnIndex).FieldName) = LCase(asFieldName) Then
                    Set Field = oFields(lnIndex)
                    Exit Property
                End If
            Next
            Set Field = New clsField
        End If
    End Property
' ------------------------------------------------------------------------------
    Public Function Exists(ByRef avKeyIndex)
        Exists = Not IndexOf(avKeyIndex) = -1
    End Function
' ------------------------------------------------------------------------------
    Public Property Get ValueOf(ByRef avKeyIndex)
        Dim lnIndex
        lnIndex = IndexOf(avKeyIndex)
        if lnIndex = -1 Then Exit Property
        ValueOf = oFields(lnIndex).Value
    End Property
' ------------------------------------------------------------------------------
    Public Property Get FileNameOf(ByRef avKeyIndex)
        Dim lnIndex
        lnIndex = IndexOf(avKeyIndex)
        if lnIndex = -1 Then Exit Property
        FileNameOf = oFields(lnIndex).FileName
    End Property
' ------------------------------------------------------------------------------
    Public Property Get LengthOf(ByRef avKeyIndex)
        Dim lnIndex
        lnIndex = IndexOf(avKeyIndex)
        if lnIndex = -1 Then Exit Property
        LengthOf = oFields(lnIndex).LengthOf
    End Property
' ------------------------------------------------------------------------------
    Public Property Get BinaryDataOf(ByRef avKeyIndex)
        Dim lnIndex
        lnIndex = IndexOf(avKeyIndex)
        if lnIndex = -1 Then Exit Property
        BinaryDataOf = oFields(lnIndex).BinaryData
    End Property
' ------------------------------------------------------------------------------
    Private Function IndexOf(ByVal avKeyIndex)
        Dim lnIndex
        If IsNumeric(asFieldName) Then
            avKeyIndex = CLng(avKeyIndex)
            If nFieldCount > avKeyIndex And avKeyIndex > -1 Then
                IndexOf = avKeyIndex
            Else
                IndexOf = -1
            End If
        Else
            For lnIndex = 0 To nFieldCount - 1
                If LCase(oFields(lnIndex).FieldName) = LCase(avKeyIndex) Then
                    IndexOf = lnIndex
                    Exit Function
                End If
            Next
            IndexOf = -1
        End If
    End Function
' ------------------------------------------------------------------------------
    Public Property Get ContentTypeOf(ByRef avKeyIndex)
        Dim lnIndex
        lnIndex = IndexOf(avKeyIndex)
        if lnIndex = -1 Then Exit Property
        ContentTypeOf = oFields(lnIndex).ContentType
    End Property
' ------------------------------------------------------------------------------
    Private Sub Class_Terminate()
        For lnIndex = 0 To nFieldCount - 1
            Set oFields(0) = Nothing
        Next
    End Sub
' ------------------------------------------------------------------------------
    Private Sub Class_Initialize()
     
        Dim lnBytes                ' Bytes received from the client
        Dim lnByteCount            ' Number of bytes received
        Dim lnStartPosition        ' Position at which content begins
        Dim lnEndPosition        ' Position at which content ends
     
        Dim loDic                ' Contains properties of each
                                ' specific field
                                ' Local dictionary object(s)
                                ' to be appended to class-scope
                                ' dictioary object.
                             
        Dim lnBoundaryBytes        ' Bytes contained within the current boundary
        Dim lnBoundaryStart        ' Position at wich the current boundary begins
                                ' within the lnBytes binary data.
        Dim lnBoundaryEnd        ' Position at wich the current boundary ends
                                ' within the lnBytes binary data.
        Dim lnDispositionPosition
     
        Dim lsFieldName            ' Name of the current field being parsed from
                                ' Binary Data
        Dim lsFileName            ' Name of the file within the current boundary
        Dim lnFileNamePosition    ' Location of file name within current boundary
     
        ' Initialize Fields
        nFieldCount = 0
        ReDim oFields(-1)
     
        ' Read the bytes (binary data) into memory 
        lnByteCount = Request.TotalBytes
        lnBytes = Request.BinaryRead(lnByteCount)
     
        'Get the lnBoundaryBytes
        lnStartPosition = 1
        lnEndPosition = InstrB(lnStartPosition, lnBytes, CStrB(vbCr))
     
        lnBoundaryBytes = MidB(lnBytes, lnStartPosition, lnEndPosition - lnStartPosition)
     
        lnBoundaryStart = InstrB(1, lnBytes, lnBoundaryBytes)
     
     
        ' Loop until the BoundaryBytes begin with "--"
        Do Until (lnBoundaryStart = InstrB(lnBytes, lnBoundaryBytes & CStrB("--")))
     
            ' All data within this boundary is stored within a local dictionary
            ' to be appended to the class-scope dictionary.
         
            ReDim Preserve oFields(nFieldCount)
            nFieldCount = nFieldCount + 1
         
            Set loField = New clsField

            lnDispositionPosition = InstrB(lnBoundaryStart, lnBytes, CStrB("Content-Disposition"))
         
            ' Get an object name
            lnStartPosition = InstrB(lnDispositionPosition, lnBytes, CStrB("name=")) + 6
            lnEndPosition = InstrB(lnStartPosition, lnBytes, CStrB(""""))
            lsFieldName = CStrU(MidB(lnBytes, lnStartPosition, lnEndPosition - lnStartPosition))
            loField.FieldName = lsFieldName
         
            ' Get the location fo the file name.
            lnFileNamePosition = InstrB(lnBoundaryStart, lnBytes, CStrB("filename="))
            lnBoundaryEnd = InstrB(lnEndPosition, lnBytes, lnBoundaryBytes)
         
            'Test if object is a file
            If Not lnFileNamePosition = 0 And lnFileNamePosition < lnBoundaryEnd Then
         
                ' Parse Filename
                lnStartPosition = lnFileNamePosition + 10
                lnEndPosition =  InstrB(lnStartPosition, lnBytes, CStrB(""""))
                lsFileName = CStrU(MidB(lnBytes,lnStartPosition,lnEndPosition-lnStartPosition))
                loField.FileName = lsFileName             
             
                ' Parse Content-Type
                lnStartPosition = InstrB(lnEndPosition,lnBytes,CStrB("Content-Type:")) + 14
                lnEndPosition = InstrB(lnStartPosition,lnBytes,CStrB(vbCr))
                ContentType = CStrU(MidB(lnBytes,lnStartPosition,lnEndPosition-lnStartPosition))
                loField.ContentType = ContentType

                ' Parse Content
                lnStartPosition = lnEndPosition + 4
                lnEndPosition = InstrB(lnStartPosition,lnBytes,lnBoundaryBytes)-2
                Value = MidB(lnBytes,lnStartPosition,lnEndPosition-lnStartPosition)
                loField.BinaryData = Value & CStrB(vbNull)
                loField.Length = LenB(Value)
            Else

                ' Parse Content
                lnStartPosition = InstrB(lnDispositionPosition, lnBytes, CStrB(vbCr)) + 4
                lnEndPosition = InstrB(lnStartPosition, lnBytes, lnBoundaryBytes) - 2
                Value = CStrU(MidB(lnBytes,lnStartPosition,lnEndPosition-lnStartPosition))
                loField.Value = Value
                loField.Length = Len(Value)
            End If

            Set oFields(UBound(oFields)) = loField

            'Loop to next object
            lnBoundaryStart = InstrB(lnBoundaryStart + LenB(lnBoundaryBytes), lnBytes, lnBoundaryBytes)
         
            Set loField = Nothing
         
        Loop

    End Sub
' ------------------------------------------------------------------------------
    Private Function CStrU(ByRef psByteString)
        Dim lnLength
        Dim lnPosition
        lnLength = LenB(psByteString)
        For lnPosition = 1 To lnLength
            CStrU = CStrU & Chr(AscB(MidB(psByteString, lnPosition, 1)))
        Next
    End Function
' ------------------------------------------------------------------------------
    Private Function CStrB(ByRef psUnicodeString)
        Dim lnLength
        Dim lnPosition
        lnLength = Len(psUnicodeString)
        For lnPosition = 1 To lnLength
            CStrB = CStrB & ChrB(AscB(Mid(psUnicodeString, lnPosition, 1)))
        Next
    End Function
' ------------------------------------------------------------------------------
End Class
' ------------------------------------------------------------------------------
%>


Asp-kode:
<!--#include file="database.asp"-->
<!--#include file="upload.class"-->

<strong>Filer</strong>
<br><br>
<% If Request.QueryString("Action") = "" Then %>
<form method="POST" enctype="multipart/form-data" action="?Action=upload&Site_Id=<%=Site_Id%>">
<table border="0" width="406" style="border-collapse: collapse" bordercolor="#111111" id="AutoNumber1">
<tr>
<td width="398"><b>Upload fil</b></td>
</tr>
<tr>
<td width="398">
<table width="100%"  border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td>Fil:</td>
    <td><input type="file" name="File1" size="20"></td>
  </tr>
  <tr>
    <td>Kaldenavn:</td>
    <td><input name="File_Name" type="text" size="17"></td>
  </tr>
</table>
  </td>
</tr>
<tr>
<td width="398"><input type="submit" value="Upload fil" name="B1"></td>
</tr>
</table>
</form>
<br>
<strong>Uploadede filer:</strong><br><br>
<%
Set rs_list_files = Server.CreateObject("ADODB.RecordSet")
rs_list_files.open "Select * From [Files] where Site_Id = " & Site_Id , Conn
%>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
<% Do while not rs_list_files.eof %>
  <tr>
    <td width="220"><img width="200" src="../userfiles/<%=Unique_Name%>/files/<%=rs_list_files("File_Url")%>"></td>
    <td valign="top"><strong><%=rs_list_files("File_Name")%></strong><br><br><a href="?Action=delete&File_Id=<%=rs_list_files("Id")%>">[Slet]</a></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
<%
rs_list_files.MoveNext
Loop
rs_list_files.close
%>
</table>
<% ElseIf Request.QueryString("Action") = "upload" Then %>
<%
Dim oUpload
Dim oFile
Dim sFileName
Dim oFSO
Dim sPath
Dim sNewData
Dim nLength
Dim bytBinaryData
Dim folder


Const nForReading = 1
Const nForWriting = 2
Const nForAppending = 8

' grab the uploaded file data
Set oUpload = New clsUpload
Set oFile = oUpload("File1")

' parse the file name
sFileName = oFile.FileName
If Not InStr(sFileName, "\") = 0 Then
    sFileName = Mid(sFileName, InStrRev(sFileName, "\") + 1)
End If

' Convert the binary data to Ascii
bytBinaryData = oFile.BinaryData
nLength = LenB(bytBinaryData)
For nIndex = 1 To nLength
    sNewData = sNewData & Chr(AscB(MidB(bytBinaryData, nIndex, 1)))
Next

Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
oFSO.OpenTextFile(folder & sFileName, nForWriting, True).Write sNewData
Set oFSO = Nothing

Response.write("Filen er uploaded!!!")

File_Name = oUpload("File_Name").Value
'Opretter billede i databasen
Set rs_add_file = Server.CreateObject("ADODB.RecordSet")
rs_add_file.open "Select * From [Files]" , Conn,1,3
rs_add_file.Addnew
rs_add_file("Site_Id")        =    Site_Id
rs_add_file("File_Name")    =    File_Name
rs_add_file("File_Url")        =    sFileName
rs_add_file.Update

Set oFile = Nothing
Set oUpload = Nothing

response.Redirect("files.asp")
%>
<% ElseIf Request.QueryString("Action") = "delete" Then

'Sletter billede fra databasen og serveren
Set rs_del_file = Server.CreateObject("ADODB.RecordSet")
rs_del_file.open "Select * From [Files] Where Id =" & request.QueryString("File_Id") , Conn,1,3
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile Server.MapPath("../userfiles/"&Unique_Name&"/files/"&rs_del_file("File_Url")&""), True
rs_del_file.Delete
rs_del_file.Update
rs_del_file.Close
response.redirect("files.asp")
%>
<% End If %>
Avatar billede busschou Praktikant
15. april 2005 - 08:38 #3
ok den kan jeg ikke lige lure hvorfor den ikke tager alle typer
Avatar billede bruhnsweb Nybegynder
15. april 2005 - 08:42 #4
det er heller ikke så meget at den skriver at typen ikke er godkendt, den kommer med nogle andre fejl, bla. script timeout.. Det som er mærkeligt er at den skriver det to gange:
Active Server Pages error 'ASP 0113'

Script timed out

/admin/files.asp

The maximum amount of time for a script to execute was exceeded. You can change this limit by specifying a new value for the property Server.ScriptTimeout or by changing the value in the IIS administration tools.

Active Server Pages error 'ASP 0113'

Script timed out

/admin/files.asp

The maximum amount of time for a script to execute was exceeded. You can change this limit by specifying a new value for the property Server.ScriptTimeout or by changing the value in the IIS administration tools.
Avatar billede bruhnsweb Nybegynder
15. april 2005 - 08:44 #5
og denne fejl:
Microsoft VBScript runtime  error '800a0046'

Permission denied

/admin/files.asp, line 88


I linje 88 står der:
oFSO.OpenTextFile(folder & sFileName, nForWriting, True).Write sNewData
Avatar billede busschou Praktikant
15. april 2005 - 08:45 #6
Avatar billede bruhnsweb Nybegynder
15. april 2005 - 08:49 #7
ja ok rettet - men der sker stadig intet.. jeg prøvede at uploade et pdf-dokument (521kb), og satte timeout til 2 min. der skete intet. Efter 2 minutter skrev den:
Active Server Pages error 'ASP 0113'

Script timed out

/admin/files.asp

The maximum amount of time for a script to execute was exceeded. You can change this limit by specifying a new value for the property Server.ScriptTimeout or by changing the value in the IIS administration tools.

MEn der sker ik rigtig noget i koden... :o(
Avatar billede bruhnsweb Nybegynder
15. april 2005 - 08:55 #8
nu har jeg prøvet at uploade et pdf der kun fylder 6kb - så skriver den med det samme:
Microsoft VBScript runtime  error '800a0046'

Permission denied

/admin/files.asp, line 89


I linje 89 står der:
oFSO.OpenTextFile(folder & sFileName, nForWriting, True).Write sNewData
Avatar billede busschou Praktikant
15. april 2005 - 08:55 #9
Jeg synes det er en voldsom kode...kan næsten ikke gennemskue den
Har din udbyder ikke en komponent installeret som fx aspSmartUpload ? for så ville det vist være pænt lettere...i mine øjne
Jeg kan i hvert fald nok ikke hjælpe dig mere med det du har desværre
Avatar billede bruhnsweb Nybegynder
15. april 2005 - 09:01 #10
Jeg har fundet fejlen :o(

Jeg havde ikke angivet en sti hvor filen skulle gemmes :o)

Doooohhh
Avatar billede busschou Praktikant
15. april 2005 - 09:05 #11
hehe ja doh
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