2

This is a method in ASP Classic that saves a file to disk. It takes a very long time but I'm not sure why. Normally, I wouldn't mind so much, but the files it handles are pretty large so need this needs to faster than 100kB a second save. Seriously slow. (old legacy system, band aid fix till it gets 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'm asking because there are plenty of WTF's in this code so I'm fighting those fires while getting some help on these ones.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
Dan
  • 12,808
  • 7
  • 45
  • 54
  • Have you tried stepping through it with a debugger to see which statement in particular is slow, or placing some kind of logging steps between every statement to see what's taking so long? I'd suspect it's a single statement that's causing most of your heartache. – SqlRyan Apr 27 '10 at 15:47
  • Off Topic: You could change your test for the trailing slash by `If Right(sPath, 1) <> "\" Then sPath = sPath & "\"`. – Filburt Apr 27 '10 at 20:20
  • I haven't placed any logging as that would include writing to a file... – Dan Apr 28 '10 at 09:45
  • @Filburt - competely useless in helping but can't help loving little code tidy ups like that :) – Dan Apr 28 '10 at 21:20
  • Have you tried comparing performance with ADODB.Stream? – Thomas Kjørnes Apr 30 '10 at 20:27
  • @thomask - No, can you post an example as an answer or do you mean save to DB? – Dan May 06 '10 at 08:19

2 Answers2

1

I don't see your definition for "FileData" anywhere in your code - where is this coming from? Is there a reason you're writing it to disk a single character at a time? I'd suspect this is your problem - writing 100K of data takes 100K trips through this loop, which could be the reason for your slowdown. 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 a single statement to write the file all at once?

oFile.Write FileData
SqlRyan
  • 33,116
  • 33
  • 114
  • 199
  • -1. This will corrupt the data. The OP has posted a very niave solution to uploading arbitary files to an ASP server where third-party dlls are not present to assist. The problem with your solution is that the `Write` method will attempt to map Unicode to an ANSI codepage where what is really desired is the storage of the content verbatim. – AnthonyWJones Apr 28 '10 at 09:46
  • With regard to this unicode vs ansi thing, I solve this using a simple lookup table (Jscript): var unicodeToAnsi = { 8364: 128, 129: 129, 8218: 130, 402: 131, 8222: 132, 8230: 133, 8224: 134, 8225: 135, 710: 136, 8240: 137, 352: 138, 8249: 139, 338: 140, 141: 141, 381: 142, 143: 143, 144: 144, 8216: 145, 8217: 146, 8220: 147, 8221: 148, 8226: 149, 8211: 150, 8212: 151, 732: 152, 8482: 153, 353: 154, 8250: 155, 339: 156, 157: 157, 382: 158, 376: 159 }; – Thomas Kjørnes May 06 '10 at 17:11
1

What you should do is read the binary request into an ADODB.Stream object and convert it to plain ASCII text in a single fast 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 variable FormData now contains the form data as text. Then you parse this text and locate the start and length of each file, and use ADODB.Stream CopyTo method to extract the specific portion of the file and save it do disk.

Thomas Kjørnes
  • 1,928
  • 1
  • 17
  • 17
  • Cheers for that, I will have a go at it soon – Dan May 07 '10 at 10:03
  • This leads to the solution, a more complete example can be found here: http://stackoverflow.com/questions/12190305/how-to-upload-files-with-asp-classic – Fabian May 13 '16 at 14:19