Avatar billede card Nybegynder
05. februar 2005 - 12:32 Der er 8 kommentarer og
1 løsning

Downloade blobs/images fra MySQL med ASP's Response.BinaryWrite

Hej jeg har stykket tre små side sammen til at uploade og downloade filer fra min database. Det fungerer fint med eks. pdf filer, men når det kommer til billeder kan scriptet kun hente 4 bytes hvilket resultere et en bronken-link billeder. Nogen der kender en løsning?


FILE_EKS.ASP

<!-- #include file="upload.asp" -->
<%
CONN_STRING = "Driver={mysql odbc 3.51 Driver};SERVER=localhost;DATABASE=db;UID=id;PASSWORD=pw;option=16387;"
SCRIPT_NAME = Request.ServerVariables("SCRIPT_NAME")

' Choose what to do by looking at the action parameter
Select Case LCase(Trim(Request.QueryString("action")))
    Case "addfile"

        'NOTE - YOU MUST HAVE VBSCRIPT v5.0 INSTALLED ON YOUR WEB SERVER
        '      FOR THIS LIBRARY TO FUNCTION CORRECTLY. YOU CAN OBTAIN IT
        '      FREE FROM MICROSOFT WHEN YOU INSTALL INTERNET EXPLORER 5.0
        '      OR LATER.
       
       
        ' Create the FileUploader
        Dim Uploader, File
        Set Uploader = New FileUploader
        Dim RS
       
        ' This starts the upload process
        Uploader.Upload()
       
        '******************************************
        ' Use [FileUploader object].Form to access
        ' additional form variables submitted with
        ' the file upload(s). (used below)
        '******************************************
       
        ' Loop through the uploaded files
        For Each File In Uploader.Files.Items
           
            ' Open the table you are saving the file to
            strSQL = "SELECT * FROM files WHERE ID=0;"
            Set RS = Server.CreateObject("ADODB.Recordset")
            RS.Open strSQL, CONN_STRING, 2, 2
            RS.AddNew ' create a new record
           
            RS("filename")    = File.FileName
            RS("filesize")      = File.FileSize
            RS("contenttype") = File.ContentType
       
            ' Save the file to the database
            File.SaveToDatabase RS("filedata")
           
            ' Commit the changes and close
            RS.Update
            RS.Close
           
        Next

        Response.Redirect(SCRIPT_NAME)


    Case "deletefile"

        ' Get the id to delete
        iRecordId = Request.QueryString("id")
        If IsNumeric(iRecordId) Then
            iRecordId = CLng(iRecordId)
        Else
            iRecordId = 0
        End If

        strSQL = "DELETE FROM files WHERE id=" & iRecordId & ";"

        Set cnnDBEdit = Server.CreateObject("ADODB.Connection")
        cnnDBEdit.Open CONN_STRING

        cnnDBEdit.Execute strSQL, adAffectAll, adExecuteNoRecords

        cnnDBEdit.Close
        Set cnnDBEdit = Nothing

        Response.Redirect(SCRIPT_NAME)


    Case Else
   
        antal = 0
        colorflip = 0
       
        ' Our default action... just lists the records in the DB
        strSQL = "SELECT id, FileName, FileSize, ContentType FROM files;"

        Set rstDBEdit = Server.CreateObject("ADODB.Recordset")
        rstDBEdit.Open strSQL, CONN_STRING, 0, 1, 1
        %>

        <table width="100%" border="1" cellpadding="3" cellspacing="0">
            <tr>
                <td>FileName</td>
                <td width="100">FileSize</td>
                <td width="150">ContentType</td>
                <td width="30">&nbsp;</td>
            </tr>
        <% Do While Not rstDBEdit.EOF %>
            <tr>
                <td><a href="download_eks.asp?id=<%= rstDBEdit.Fields("id").Value %>" ><%= rstDBEdit.Fields("FileName").Value %></a></td>
                <td><%= rstDBEdit.Fields("FileSize").Value %></td>
                <td><%= rstDBEdit.Fields("ContentType").Value %></td>
                <td><a onclick="return confirmSubmit()" href="<%= SCRIPT_NAME %>?action=deletefile&id=<%= rstDBEdit.Fields("id").Value %>"><img src="wrong.gif" alt="slet fil" width="16" height="13" border="0" style="border: 0;"></a></td>
            </tr>
            <%
            rstDBEdit.MoveNext
        Loop
        %>
        <tr>
            <td colspan="4" align="center"><br>
                <FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="<%= SCRIPT_NAME %>?action=addfile">
                    <TABLE BORDER=0 cellpadding="0" cellspacing="0">
                    <tr>
                        <td><b>Hent filer:</b><br><INPUT TYPE="FILE" SIZE="40" NAME="FILE1"></td>
                    </tr>
                    <tr>
                        <td align="center"><INPUT TYPE="SUBMIT" VALUE="Gem"></td>
                    </tr>
                    </TABLE>
                </FORM>
            </td>
        </tr>
        </table>

        <%
        rstDBEdit.Close
        Set rstDBEdit = Nothing
       

End Select
%>

DOWNLOAD_EKS.ASP
<%
    response.Expires = 0
    response.Buffer  = True
    response.Clear

    iRecordId = Request.QueryString("id")
    If IsNumeric(iRecordId) Then
        iRecordId = CLng(iRecordId)
    Else
        iRecordId = 0
    End If

    CONN_STRING = "Driver={mysql odbc 3.51 Driver};SERVER=localhost;DATABASE=db;UID=id;PASSWORD=pw;option=16387;"

    strSQL = "SELECT * FROM files WHERE ID="& iRecordId &";"
    Set RS = Server.CreateObject("ADODB.Recordset")
    RS.Open strSQL, CONN_STRING, 2, 3
   
    Response.ContentType = "image/jpeg" 'RS("contenttype")
    Response.AddHeader "Content-Disposition", "attachment; filename="& RS("FileName") &""
    Response.BinaryWrite RS("filedata")

    RS.Close
    Set RS = Nothing

    response.End
%>


UPLOAD.ASP

<%
'***************************************
' File:      Upload.asp
' Author: Jacob "Beezle" Gilley
' Email:  avis7@airmail.net
' Date:  12/07/2000
' Comments: The code for the Upload, CByteString,
'            CWideString    subroutines was originally
'            written by Philippe Collignon...or so
'            he claims. Also, I am not responsible
'            for any ill effects this script may
'            cause and provide this script "AS IS".
'            Enjoy!
'****************************************

Class FileUploader
    Public  Files
    Private mcolFormElem

    Private Sub Class_Initialize()
        Set Files = Server.CreateObject("Scripting.Dictionary")
        Set mcolFormElem = Server.CreateObject("Scripting.Dictionary")
    End Sub
   
    Private Sub Class_Terminate()
        If IsObject(Files) Then
            Files.RemoveAll()
            Set Files = Nothing
        End If
        If IsObject(mcolFormElem) Then
            mcolFormElem.RemoveAll()
            Set mcolFormElem = Nothing
        End If
    End Sub

    Public Property Get Form(sIndex)
        Form = ""
        If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
    End Property

    Public Default Sub Upload()
        Dim biData, sInputName
        Dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos
        Dim nPosFile, nPosBound

        biData = Request.BinaryRead(Request.TotalBytes)
        nPosBegin = 1
        nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
       
        If (nPosEnd-nPosBegin) <= 0 Then Exit Sub
       
        vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
        nDataBoundPos = InstrB(1, biData, vDataBounds)
       
        Do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString("--"))
           
            nPos = InstrB(nDataBoundPos, biData, CByteString("Content-Disposition"))
            nPos = InstrB(nPos, biData, CByteString("name="))
            nPosBegin = nPos + 6
            nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
            sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
            nPosFile = InstrB(nDataBoundPos, biData, CByteString("filename="))
            nPosBound = InstrB(nPosEnd, biData, vDataBounds)
           
            If nPosFile <> 0 And  nPosFile < nPosBound Then
                Dim oUploadFile, sFileName
                Set oUploadFile = New UploadedFile
               
                nPosBegin = nPosFile + 10
                nPosEnd =  InstrB(nPosBegin, biData, CByteString(Chr(34)))
                sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
                oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, "\"))

                nPos = InstrB(nPosEnd, biData, CByteString("Content-Type:"))
                nPosBegin = nPos + 14
                nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
               
                oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
               
                nPosBegin = nPosEnd+4
                nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
                oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
               
                If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile
            Else
                nPos = InstrB(nPos, biData, CByteString(Chr(13)))
                nPosBegin = nPos + 4
                nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
                If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
            End If

            nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds)
        Loop
    End Sub

    'String to byte string conversion
    Private Function CByteString(sString)
        Dim nIndex
        For nIndex = 1 to Len(sString)
          CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1)))
        Next
    End Function

    'Byte string to string conversion
    Private Function CWideString(bsString)
        Dim nIndex
        CWideString =""
        For nIndex = 1 to LenB(bsString)
          CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1)))
        Next
    End Function
End Class

Class UploadedFile
    Public ContentType
    Public FileName
    Public FileData
   
    Public Property Get FileSize()
        FileSize = LenB(FileData)
    End Property

    Public Sub SaveToDisk(sPath)
        Dim oFS, oFile
        Dim nIndex
   
        If sPath = "" Or FileName = "" Then Exit Sub
        If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\"
   
        Set oFS = Server.CreateObject("Scripting.FileSystemObject")
        If Not oFS.FolderExists(sPath) Then Exit Sub
       
        Set oFile = oFS.CreateTextFile(sPath & FileName, True)
       
        For nIndex = 1 to LenB(FileData)
            oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
        Next

        oFile.Close
    End Sub
   
    Public Sub SaveToDatabase(ByRef oField)
        If LenB(FileData) = 0 Then Exit Sub
       
        If IsObject(oField) Then
            oField.AppendChunk FileData
        End If
    End Sub

End Class
%>
Avatar billede card Nybegynder
05. februar 2005 - 16:50 #1
Kom nu...  Er der ikke nogen som tør prøve kræfter med denne her lille udfordring?
Jeg tror det er et ret nyttigt script hvis småfejlene bliver eliminieret...
Avatar billede ldanielsen Nybegynder
07. februar 2005 - 10:13 #2
Fint script, har du skrevet det selv?

Fortæl mig lige, bliver der kun gemt 4 bytes, eller kan du kun læse 4 bytes?
Avatar billede card Nybegynder
07. februar 2005 - 12:33 #3
Jeg har stjålet lidt hist og pist, men det kan ses i scriptet. Eller har jeg skrevet en del af det selv.

Der bliver kun gemt 4 bytes, når jeg downloader. Upload fungere fint. Alt hvad der uploades gemmes korrekt i databasen og kan enten ses direkte i databasen eller gemmes til disk. Jeg kan ikke finde ud af om MySQL tilføjer en oleheader i stil med access eller hvad der er galt.

Jeg fået scriptet til at virke hvis det i stedet gemmer filerne på disken, men det er jo ikke helt det samme.
Avatar billede card Nybegynder
07. februar 2005 - 13:42 #4
Her en er filemanager, som lægger hver fil ned i en unik mappe på drevet, så der ikke opstår navnekonflikt. Er nødvendigt, hvis flere brugere skal kunne uploade uden at de kan se hinandens filer.


>>>>>>>>>> FILEMANAGER.SQL

#----------------------------
# Table structure for files
#----------------------------
create table filemanager (
  id int(11) not null auto_increment,
  FileName varchar(50) not null,
  FileSize varchar(50) not null,
  ContentType varchar(50) not null,
  primary key (id))
  type=MyISAM;

>>>>>>>>FILEMANAGER.ASP

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Filemanager</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script LANGUAGE="JavaScript">
<!--

function confirmSubmit()
{
var agree=confirm("Er du sikker på du vil fortsætte?");
if (agree)
    return true ;
else
    return false ;
}
// -->
</script>

</head>

<body text="#000000" link="#000000" vlink="#000000" alink="#000000">

<%

' husk at oprette mappen "uploads" ellers opstår der fejl.
strPath = "\filemanager\uploads"
CONN_STRING = "Driver={mysql odbc 3.51 Driver};SERVER=localhost;DATABASE=db;UID=brugernavn;PASSWORD=pw;option=16387;"
SCRIPT_NAME = Request.ServerVariables("SCRIPT_NAME")

%>

<%
'***************************************
' File:      Upload.asp
' Author: Jacob "Beezle" Gilley
' Email:  avis7@airmail.net
' Date:  12/07/2000
' Comments: The code for the Upload, CByteString,
'            CWideString    subroutines was originally
'            written by Philippe Collignon...or so
'            he claims. Also, I am not responsible
'            for any ill effects this script may
'            cause and provide this script "AS IS".
'            Enjoy!
'****************************************

Class FileUploader
    Public  Files
    Private mcolFormElem

    Private Sub Class_Initialize()
        Set Files = Server.CreateObject("Scripting.Dictionary")
        Set mcolFormElem = Server.CreateObject("Scripting.Dictionary")
    End Sub
   
    Private Sub Class_Terminate()
        If IsObject(Files) Then
            Files.RemoveAll()
            Set Files = Nothing
        End If
        If IsObject(mcolFormElem) Then
            mcolFormElem.RemoveAll()
            Set mcolFormElem = Nothing
        End If
    End Sub

    Public Property Get Form(sIndex)
        Form = ""
        If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
    End Property

    Public Default Sub Upload()
        Dim biData, sInputName
        Dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos
        Dim nPosFile, nPosBound

        biData = Request.BinaryRead(Request.TotalBytes)
        nPosBegin = 1
        nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
       
        If (nPosEnd-nPosBegin) <= 0 Then Exit Sub
       
        vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
        nDataBoundPos = InstrB(1, biData, vDataBounds)
       
        Do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString("--"))
           
            nPos = InstrB(nDataBoundPos, biData, CByteString("Content-Disposition"))
            nPos = InstrB(nPos, biData, CByteString("name="))
            nPosBegin = nPos + 6
            nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
            sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
            nPosFile = InstrB(nDataBoundPos, biData, CByteString("filename="))
            nPosBound = InstrB(nPosEnd, biData, vDataBounds)
           
            If nPosFile <> 0 And  nPosFile < nPosBound Then
                Dim oUploadFile, sFileName
                Set oUploadFile = New UploadedFile
               
                nPosBegin = nPosFile + 10
                nPosEnd =  InstrB(nPosBegin, biData, CByteString(Chr(34)))
                sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
                oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, "\"))

                nPos = InstrB(nPosEnd, biData, CByteString("Content-Type:"))
                nPosBegin = nPos + 14
                nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
               
                oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
               
                nPosBegin = nPosEnd+4
                nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
                oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
               
                If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile
            Else
                nPos = InstrB(nPos, biData, CByteString(Chr(13)))
                nPosBegin = nPos + 4
                nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
                If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
            End If

            nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds)
        Loop
    End Sub

    'String to byte string conversion
    Private Function CByteString(sString)
        Dim nIndex
        For nIndex = 1 to Len(sString)
          CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1)))
        Next
    End Function

    'Byte string to string conversion
    Private Function CWideString(bsString)
        Dim nIndex
        CWideString =""
        For nIndex = 1 to LenB(bsString)
          CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1)))
        Next
    End Function
End Class

Class UploadedFile
    Public ContentType
    Public FileName
    Public FileData
   
    Public Property Get FileSize()
        FileSize = LenB(FileData)
    End Property

    Public Sub SaveToDisk(sPath)
        Dim oFS, oFile
        Dim nIndex
   
        If sPath = "" Or FileName = "" Then Exit Sub
        If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\"
   
        Set oFS = Server.CreateObject("Scripting.FileSystemObject")
        If Not oFS.FolderExists(sPath) Then Exit Sub
       
        Set oFile = oFS.CreateTextFile(sPath & FileName, True)
       
        For nIndex = 1 to LenB(FileData)
            oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
        Next

        oFile.Close
    End Sub
   
    Public Sub SaveToDatabase(ByRef oField)
        If LenB(FileData) = 0 Then Exit Sub
       
        If IsObject(oField) Then
            oField.AppendChunk FileData
        End If
    End Sub

End Class


' Choose what to do by looking at the action parameter

Select Case LCase(Trim(Request.QueryString("action")))
    Case "addfile"

        ' Get the id
        iRecordId = Request.QueryString("id")
        If IsNumeric(iRecordId) Then
            iRecordId = CLng(iRecordId)
        Else
            iRecordId = 0
        End If


        'NOTE - YOU MUST HAVE VBSCRIPT v5.0 INSTALLED ON YOUR WEB SERVER
        '      FOR THIS LIBRARY TO FUNCTION CORRECTLY. YOU CAN OBTAIN IT
        '      FREE FROM MICROSOFT WHEN YOU INSTALL INTERNET EXPLORER 5.0
        '      OR LATER.
       
       
        ' Create the FileUploader
        Dim Uploader, File
        Set Uploader = New FileUploader
        Dim RS
       
        ' This starts the upload process
        Uploader.Upload()
       
        '******************************************
        ' Use [FileUploader object].Form to access
        ' additional form variables submitted with
        ' the file upload(s). (used below)
        '******************************************
       
        ' Loop through the uploaded files
        For Each File In Uploader.Files.Items
           
            ' Open the table you are saving the file to
            strSQL = "SELECT * FROM filemanager WHERE ID=0;"
            Set RS = Server.CreateObject("ADODB.Recordset")
            RS.CursorLocation = 3
            RS.Open strSQL, CONN_STRING, 1, 2, 1

            RS.AddNew ' create a new record
            RS.Fields("filename").Value                      = File.FileName
            RS.Fields("filesize").Value                        = File.FileSize
            RS.Fields("contenttype").Value                  = File.ContentType
            RS.Update

            ' henter nyoprettede id           
            strFolderId = RS.Fields("id").Value 

            ' Lukker forbindelse
            RS.Close


            ' Opretter en unik mappe
            strFolder = Server.Mappath(strPath) & "\" & strFolderId
           
            Set fso = CreateObject("Scripting.FileSystemObject")
            Set objFolder = fso.CreateFolder(strFolder)
            Set fso = Nothing

            ' Save the file to disk
            File.SaveToDisk strFolder

           
        Next

        Response.Redirect(SCRIPT_NAME & "?id=" & Request.QueryString("id"))


    Case "deletefile"

        ' Get the id to delete
        iRecordId = Request.QueryString("deleteid")
        If IsNumeric(iRecordId) Then
            iRecordId = CLng(iRecordId)
        Else
            iRecordId = 0
        End If

        strSQL = "DELETE FROM filemanager WHERE id=" & iRecordId & ";"

        Set cnnDBEdit = Server.CreateObject("ADODB.Connection")
        cnnDBEdit.Open CONN_STRING

        cnnDBEdit.Execute strSQL, adAffectAll, adExecuteNoRecords

        cnnDBEdit.Close
        Set cnnDBEdit = Nothing

        ' bygger sti til mappe der skal slettes
        strFolder = Server.Mappath(strPath) & "\" & iRecordId

        ' sletter mappe med fil
        Set fso = CreateObject("Scripting.FileSystemObject")
        fso.DeleteFolder strFolder
        Set fso = Nothing

        Response.Redirect(SCRIPT_NAME & "?id=" & Request.QueryString("id"))

Case "download"

        '8***********************************************8
        ' Jason Withrow - For ASP101 July 2001
        ' This page forces the save as dialogue to prevent
        ' files from being opened in the browser.
        '
        ' jwithrow@mediaone.net
        '8***********************************************8
   
        Response.Buffer = True
        Dim strFilePath, strFileSize, strFileName
       
        Const adTypeBinary = 1
       
       
        ' Get the id to fetch
        iRecordId = Request.QueryString("id")
        If IsNumeric(iRecordId) Then
            iRecordId = CLng(iRecordId)
        Else
            iRecordId = 0
        End If
       
        strSQL = "SELECT * FROM filemanager WHERE id="& iRecordId &";"
        Set rstDBEdit = Server.CreateObject("ADODB.Recordset")
        rstDBEdit.Open strSQL, CONN_STRING, 0, 1, 1
       
        strFilePath = Server.Mappath(strPath) & "\" & rstDBEdit.Fields("id").Value & "\" & rstDBEdit.Fields("FileName").Value
        strFileSize = rstDBEdit.Fields("FileSize").Value
        strFileName = rstDBEdit.Fields("FileName").Value
       
        rstDBEdit.Close
        Set rstDBEdit = Nothing
       
        Response.Clear
       
        '8*******************************8
        ' Requires MDAC 2.5 to be stable
        ' I recommend MDAC 2.6 or 2.7
        '8*******************************8
        Set objStream = Server.CreateObject("ADODB.Stream")
        objStream.Open
        objStream.Type = adTypeBinary
        objStream.LoadFromFile strFilePath
       
        strFileType = lcase(Right(strFileName, 4))
       
        ' Feel Free to Add Your Own Content-Types Here
        Select Case strFileType
            Case ".asf"
                ContentType = "video/x-ms-asf"
            Case ".avi"
                ContentType = "video/avi"
            Case ".doc"
                ContentType = "application/msword"
            Case ".zip"
                ContentType = "application/zip"
            Case ".xls"
                ContentType = "application/vnd.ms-excel"
            Case ".gif"
                ContentType = "image/gif"
            Case ".jpg", "jpeg"
                ContentType = "image/jpeg"
            Case ".wav"
                ContentType = "audio/wav"
            Case ".mp3"
                ContentType = "audio/mpeg3"
            Case ".mpg", "mpeg"
                ContentType = "video/mpeg"
            Case ".rtf"
                ContentType = "application/rtf"
            Case ".htm", "html"
                ContentType = "text/html"
            Case ".asp"
                ContentType = "text/asp"
            Case Else
                'Handle All Other Files
                ContentType = "application/octet-stream"
        End Select
       
       
        Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
        Response.AddHeader "Content-Length", strFileSize
        ' In a Perfect World, Your Client would also have UTF-8 as the default
        ' In Their Browser
        Response.Charset = "UTF-8"
        Response.ContentType = ContentType
       
        Response.BinaryWrite objStream.Read
        Response.Flush
   
        objStream.Close
        Set objStream = Nothing
   
   
Case Else
   
        iRecordId = Request.QueryString("id")
        If IsNumeric(iRecordId) Then
            iRecordId = CLng(iRecordId)
        Else
            iRecordId = 0
        End If
       
        ' Our default action... just lists the records in the DB
        strSQL = "SELECT * FROM filemanager;"

        Set rstDBEdit = Server.CreateObject("ADODB.Recordset")
        rstDBEdit.Open strSQL, CONN_STRING, 0, 1, 1
        %>
        <br>
        <table width="80%" border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000">
            <tr bgcolor="#639AB8">
                <td><strong>Filnavn</strong></td>
                <td width="100"><strong>Størrelse</strong></td>
                <td width="150"><strong>Filtype</strong></td>
                <td width="30"><strong>Slet</strong></td>
            </tr>
        <% Do While Not rstDBEdit.EOF %>
            <tr>
                <td><a href="<%= SCRIPT_NAME %>?action=download&id=<%= rstDBEdit.Fields("id").Value %>" ><%= rstDBEdit.Fields("FileName").Value %></a></td>
                <td><%= rstDBEdit.Fields("FileSize").Value %></td>
                <td><%= rstDBEdit.Fields("ContentType").Value %></td>
                <td align="center"><a onclick="return confirmSubmit()" href="<%= SCRIPT_NAME %>?action=deletefile&id=<%= iRecordId %>&deleteid=<%= rstDBEdit.Fields("id").Value %>">slet</a></td>
            </tr>
            <%
            rstDBEdit.MoveNext
        Loop
        %>
        <tr>
            <td colspan="4" align="center"><br>
                <FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="<%= SCRIPT_NAME %>?action=addfile&id=<%= iRecordId %>">
                    <TABLE BORDER=0 cellpadding="0" cellspacing="0">
                    <tr>
                        <td><b>Find fil:</b><br><INPUT TYPE="FILE" SIZE="40" NAME="FILE1" style="cursor: hand;"></td>
                    </tr>
                    <tr>
                        <td align="center"><br><INPUT TYPE="SUBMIT" VALUE="Upload fil" style="cursor: hand;"></td>
                    </tr>
                    </TABLE>
                </FORM>
            </td>
        </tr>
        </table>
        <br>
        <%
        rstDBEdit.Close
        Set rstDBEdit = Nothing
       

End Select
%>

</body>
</html>
Avatar billede ldanielsen Nybegynder
08. februar 2005 - 11:54 #5
Upload fungerer fint, dvs. at billederne bliver gemt korrekt i databasen.

Så problemet er at hente dem ud igen, ok?

Så lad mig se den kode der skulle hente billederne, og som fejler. Jeg har selv lavet noget tilsvarende med MSSQL, så jeg tror godt jeg kan klare den
Avatar billede card Nybegynder
08. februar 2005 - 12:03 #6
Jamen downloadkoden står der allerede... Men kan da godt lige fremhæve den her...

Tror at problemet ligger i den måde, som mysql gemmer blobs - det siger min fornemmelse mig... Har læst mig til at f.eks. access tilføjer en oleheader til informationen, som skal fjernes før det igen kan hentes ud.

DOWNLOAD_EKS.ASP
<%
    response.Expires = 0
    response.Buffer  = True
    response.Clear

    iRecordId = Request.QueryString("id")
    If IsNumeric(iRecordId) Then
        iRecordId = CLng(iRecordId)
    Else
        iRecordId = 0
    End If

    CONN_STRING = "Driver={mysql odbc 3.51 Driver};SERVER=localhost;DATABASE=db;UID=id;PASSWORD=pw;option=16387;"

    strSQL = "SELECT * FROM files WHERE ID="& iRecordId &";"
    Set RS = Server.CreateObject("ADODB.Recordset")
    RS.Open strSQL, CONN_STRING, 2, 3
   
    Response.ContentType = "image/jpeg" 'RS("contenttype")
    Response.AddHeader "Content-Disposition", "attachment; filename="& RS("FileName") &""
    Response.BinaryWrite RS("filedata")

    RS.Close
    Set RS = Nothing

    response.End
%>
Avatar billede ldanielsen Nybegynder
08. februar 2005 - 13:50 #7
Det ligner godtnok min kode en del, den eneste forskel jeg kan se er parametrene til RS.Open. Prøv lige dette:

RS.Open strSQL, CONN_STRING, 0, 1
Avatar billede card Nybegynder
08. februar 2005 - 14:33 #8
Ja det undrer mig at jeg kun har 2 parametrer der, når jeg normalt har 3. Og ja det virkede næsten... billederne kommer nu halvt igennem ... Men det har jeg læst om et eller andet sted... Så langt så godt...
Avatar billede card Nybegynder
30. januar 2011 - 14:58 #9
Fandt selv en løsning
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



Seneste spørgsmål Seneste aktivitet
I går 23:37 Poe strøm Af lurup i LAN/WAN
I går 14:46 GIF-EDITOR Af snestrup2000 i Billedbehandling
I går 14:03 Logge ind Af Bob i PC
I går 12:12 2 skærme - 1 virker - den anden siger No signal Af eksmojo i Skærme
I går 10:33 openvpn projekt Af dcedata1977 i Windows