Search code examples
asp-classicfilesystemobject

Save binary file that has been auto-generated in classic asp


I have a perl script that generates a midi file in the browser. I would like to run a classic asp script that binary reads the outputted midi file and saves it to the server. The file is however generated using a querystring, which it is not possible to read into the FileSystemObject using GetFile

What I would like is something like

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("1090.pl?chord=C&chord_id=1090")

This however doesn't work. I'm looking for ideas or other ways how to read this auto-generated midi file into a binary object which I can then save to the server


Solution

  • You can't use the file system object for this purpose, instead you can use the XMLHTTP object. Here's an example of how to use it. Note you will need to change strFileURL (the URL for your 1090.pl script) and strHDLocation (where to save on the server - you will also need to ensure the website has read/write permissions to whatever location you choose).

    <% 
    ' Set your settings 
    strFileURL = "http://localhost/1090.pl?chord=C&chord_id=1090" 
    strHDLocation = "c:\filename.ext"
    
    ' Fetch the file 
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    
    objXMLHTTP.open "GET", strFileURL, false 
    objXMLHTTP.send()
    
    If objXMLHTTP.Status = 200 Then 
        Set objADOStream = CreateObject("ADODB.Stream") 
        objADOStream.Open 
        objADOStream.Type = 1 'adTypeBinary
    
        objADOStream.Write objXMLHTTP.ResponseBody 
        objADOStream.Position = 0 'Set the stream position to the start
    
        Set objFSO = Createobject("Scripting.FileSystemObject") 
        If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation 
        Set objFSO = Nothing
    
        objADOStream.saveToFile strHDLocation 
        objADOStream.Close 
        Set objADOStream = Nothing 
    End if
    
    Set objXMLHTTP = Nothing 
    %>