ASP SaveToDisk takes an incredible amount of time

This is a method in ASP Classic that saves a file to disk. It takes a very long time, but I don't know why. Generally, I wouldn't mind that much, but the files it processes are quite large, so it would take over 100KB per second to do this. Seriously slow. (old legacy system, fixing the strip until it is replaced ...)

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

      

I ask because there are a lot of WTFs in this code, so I am struggling with these lights while getting some help from these.

+2


a source to share


2 answers


I don't see your definition for "FileData" anywhere in your code - where does this come from? Is there a reason why you write it to disk one character at a time? I suspect this is your problem - writing 100K of data requires 100K trips through this loop, which may be causing you to slow down. Why can't you replace the write loop at the bottom:

For nIndex = 1 to LenB(FileData)
    oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
Next

      



with one statement to write a file all at once?

oFile.Write FileData

      

+1


a source


What you need to do is read the binary query into an ADODB.Stream object and convert it to plain ASCII text in one quick step.

Set objStream = Server.CreateObject("ADODB.Stream")

    objStream.Type = 1
    objStream.Open
    objStream.Write Request.BinaryRead(Request.TotalBytes)
    objStream.Position = 0
    objStream.Type = 2
    objStream.Charset = "ISO-8859-1"

    FormData = objStream.ReadText

    objStream.Close

Set objStream = Nothing

      



Notice how the FormData variable now contains the form data as text. You will then parse that text and determine the start and length of each file and use the ADODB.Stream CopyTo method to extract a specific portion of the file and save it to disk.

+1


a source







All Articles