Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

viewing files problem

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
Greetings alls...
I'm using this code to display certain files, but whenever the filename contains a "," then I get an error on following line whichfile = server.mappath("poems/"&whichfile)
how do I change the code so that ALL files with valid windows charachters in it can be displayed as well?

Code:
whichfile = RS("filename")
Call ReadDisplayFile(whichfile)
SUB ReadDisplayFile(FileToRead)
    whichfile = server.mappath("poems/"&whichfile)
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set thisfile = fs.OpenTextFile(whichfile, 1, False)
    tempSTR = thisfile.readall
    response.write tempSTR
    thisfile.Close
    set thisfile=nothing
    set fs=nothing
end sub
My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Why not just take the ',' out, I would say its not a great way to name your files on a server, Dash and underscore are used to seperate words in a file name. Least its something i was always told Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
im gonna assume what ur trying to do - in your database have a field for the poem name (which can contain any character you want) also have a field for the filename (strip out all commas, spaces etc). When displaying the filename you use the real poem name field and assign the <a href> tag with the second field that contains the file name (less any invalid characters) Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
that I could do, but I found another problem... it won't accept non ASCII characters either, such as swedish Å, Ä, Ö... so I think I'll let them have the same filename as the primary key value from the database, but how do I with least truble and easiest way find the last record set without to much fuzz? My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Well, not realy as I also have in the Database information about it's title, author Genre, and few more. My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
How are you creating the files, does someone fill in a text area on the web, enter a title etc and then a .txt file is created? How are you assigning the text file name? Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
Since I didn't know my self how to enable users to upload files to my server I searched the net and found a code that I'm using right now, slightly modified though.

User enters:
Title,
Authoer,
Genre,
and selects a file on their computer

then hits submit

The program then uploads it to the poems directory, and a code snippet saves the info to the database. My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Without knowing the code, can you intercept before the file is saved to the server so that you can name the file to anything you want. Incidentally to create a txt file its something like this...

set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
set file = fs.CreateTextFile(server.mappath(&quot;/poems/10312000.txt&quot;), true, false)
file.WriteLine(&quot;Hello World!&quot;)
file.Close Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
I should be able to, but I'm not familiar with VB
what I would need to do so, is to know the next ID (the primary key, autonumber)

as this is not my code, and I know way to little about vb, I'm not sure where I should make the changes

----INSERT.ASP----
<% ' Insert.asp %>
<!--#include file=&quot;Loader.asp&quot;-->
<%
Response.Buffer = True

' load object
Dim load
Set load = new Loader

' calling initialize method
load.initialize

' File binary data
Dim fileData
fileData = load.getFileData(&quot;file&quot;)
' File name
Dim fileName
fileName = LCase(load.getFileName(&quot;file&quot;))
' File path
Dim filePath
filePath = load.getFilePath(&quot;file&quot;)
' File path complete
Dim filePathComplete
filePathComplete = load.getFilePathComplete(&quot;file&quot;)
' File size
Dim fileSize
fileSize = load.getFileSize(&quot;file&quot;)
' File size translated
Dim fileSizeTranslated
fileSizeTranslated = load.getFileSizeTranslated(&quot;file&quot;)
' Content Type
Dim contentType
contentType = load.getContentType(&quot;file&quot;)
' No. of Form elements
Dim countElements
countElements = load.Count

Dim Title
Title = load.getValue(&quot;title&quot;)
Dim Author
Author = load.getValue(&quot;author&quot;)
Dim Genre
Genre = load.getValue(&quot;genre&quot;)

' Path where file will be uploaded
Dim pathToFile
pathToFile = Server.mapPath(&quot;/poems/&quot;) & &quot;\&quot; & fileName
' Uploading file data

Dim fileUploaded
fileUploaded = load.saveToFile (&quot;file&quot;, pathToFile)

' destroying load object
Set load = Nothing
%>


--- LOADER.ASP
Code:
<%
  ' -- Loader.asp --
  ' -- version 1.5
  ' -- last updated 6/13/2002

  ' Faisal Khan
  ' faisal@stardeveloper.com
  ' [URL unfurl="true"]www.stardeveloper.com[/URL]
  ' Class for handling binary uploads

  Class Loader
    Private dict

    Private Sub Class_Initialize
      Set dict = Server.CreateObject(&quot;Scripting.Dictionary&quot;)
    End Sub

    Private Sub Class_Terminate
      If IsObject(intDict) Then
        intDict.RemoveAll
        Set intDict = Nothing
      End If
      If IsObject(dict) Then
        dict.RemoveAll
        Set dict = Nothing
      End If
    End Sub

    Public Property Get Count
      Count = dict.Count
    End Property

    Public Sub Initialize
      If Request.TotalBytes > 0 Then
        Dim binData
          binData = Request.BinaryRead(Request.TotalBytes)
          getData binData
      End If
    End Sub
    
    Public Function getFileData(name)
      If dict.Exists(name) Then
        getFileData = dict(name).Item(&quot;Value&quot;)
        Else
        getFileData = &quot;&quot;
      End If
    End Function

    Public Function getValue(name)
      Dim gv
      If dict.Exists(name) Then
        gv = CStr(dict(name).Item(&quot;Value&quot;))
        
        gv = Left(gv,Len(gv)-2)
        getValue = gv
      Else
        getValue = &quot;&quot;
      End If
    End Function
    
    Public Function saveToFile(name, path)
      If dict.Exists(name) Then
        Dim temp
          temp = dict(name).Item(&quot;Value&quot;)
        Dim fso
          Set fso = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
        Dim file
          Set file = fso.CreateTextFile(path)
            For tPoint = 1 to LenB(temp)
                file.Write Chr(AscB(MidB(temp,tPoint,1)))
            Next
            file.Close
          saveToFile = True
      Else
          saveToFile = False
      End If
    End Function

    Public Function getFileName(name)
      If dict.Exists(name) Then
        Dim temp, tempPos
          temp = dict(name).Item(&quot;FileName&quot;)
          tempPos = 1 + InStrRev(temp, &quot;\&quot;)
          getFileName = Mid(temp, tempPos)
      Else
        getFileName = &quot;&quot;
      End If
    End Function

    Public Function getFilePath(name)
      If dict.Exists(name) Then
        Dim temp, tempPos
          temp = dict(name).Item(&quot;FileName&quot;)
          tempPos = InStrRev(temp, &quot;\&quot;)
          getFilePath = Mid(temp, 1, tempPos)
      Else
        getFilePath = &quot;&quot;
      End If
    End Function

    Public Function getFilePathComplete(name)
      If dict.Exists(name) Then
        getFilePathComplete = dict(name).Item(&quot;FileName&quot;)
      Else
        getFilePathComplete = &quot;&quot;
      End If
    End Function

    Public Function getFileSize(name)
      If dict.Exists(name) Then
        getFileSize = LenB(dict(name).Item(&quot;Value&quot;))
      Else
        getFileSize = 0
      End If
    End Function

    Public Function getFileSizeTranslated(name)
      If dict.Exists(name) Then
        temp = 1 + LenB(dict(name).Item(&quot;Value&quot;))
          If Len(temp) <= 3 Then
            getFileSizeTranslated = temp & &quot; bytes&quot;
          ElseIf Len(temp) > 6 Then
            temp = FormatNumber(((temp / 1024) / 1024), 2)
            getFileSizeTranslated = temp & &quot; megabytes&quot;  
          Else
            temp = FormatNumber((temp / 1024), 2)
            getFileSizeTranslated = temp & &quot; kilobytes&quot;
          End If
      Else
        getFileSizeTranslated = &quot;&quot;
      End If
    End Function

    Public Function getContentType(name)
      If dict.Exists(name) Then
        getContentType = dict(name).Item(&quot;ContentType&quot;)
      Else
        getContentType = &quot;&quot;
      End If
    End Function

  Private Sub getData(rawData)
    Dim separator 
      separator = MidB(rawData, 1, InstrB(1, rawData, ChrB(13)) - 1)

    Dim lenSeparator
      lenSeparator = LenB(separator)

    Dim currentPos
      currentPos = 1
    Dim inStrByte
      inStrByte = 1
    Dim value, mValue
    Dim tempValue
      tempValue = &quot;&quot;

    While inStrByte > 0
      inStrByte = InStrB(currentPos, rawData, separator)
      mValue = inStrByte - currentPos

      If mValue > 1 Then
        value = MidB(rawData, currentPos, mValue)

        Dim begPos, endPos, midValue, nValue
        Dim intDict
          Set intDict = Server.CreateObject(&quot;Scripting.Dictionary&quot;)
    
          begPos = 1 + InStrB(1, value, ChrB(34))
          endPos = InStrB(begPos + 1, value, ChrB(34))
          nValue = endPos

        Dim nameN
          nameN = MidB(value, begPos, endPos - begPos)

        Dim nameValue, isValid
          isValid = True
          
          If InStrB(1, value, stringToByte(&quot;Content-Type&quot;)) > 1 Then

            begPos = 1 + InStrB(endPos + 1, value, ChrB(34))
            endPos = InStrB(begPos + 1, value, ChrB(34))
  
            If endPos = 0 Then
              endPos = begPos + 1
              isValid = False
            End If
            
            midValue = MidB(value, begPos, endPos - begPos)
              intDict.Add &quot;FileName&quot;, trim(byteToString(midValue))

        begPos = 14 + InStrB(endPos + 1, value, stringToByte(&quot;Content-Type:&quot;))
            endPos = InStrB(begPos, value, ChrB(13))

            midValue = MidB(value, begPos, endPos - begPos)
              intDict.Add &quot;ContentType&quot;, trim(byteToString(midValue))

            begPos = endPos + 4
            endPos = LenB(value)

            nameValue = MidB(value, begPos, endPos - begPos)
          Else
            nameValue = trim(byteToString(MidB(value, nValue + 5)))
          End If

          If isValid = true Then
            intDict.Add &quot;Value&quot;, nameValue
            intDict.Add &quot;Name&quot;, nameN

            dict.Add byteToString(nameN), intDict
          End If
      End If

      currentPos = lenSeparator + inStrByte
    Wend
  End Sub
  
  End Class

  Private Function stringToByte(toConv)
    Dim tempChar
     For i = 1 to Len(toConv)
       tempChar = Mid(toConv, i, 1)
      stringToByte = stringToByte & chrB(AscB(tempChar))
     Next
  End Function

  Private Function byteToString(toConv)
    For i = 1 to LenB(toConv)
      byteToString = byteToString & chr(AscB(MidB(toConv,i,1))) 
    Next
  End Function
%>
My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Quickly looking thru the code, at this point

' File name
Dim fileName
fileName = LCase(load.getFileName(&quot;file&quot;))

is where you could rename the file, my suggestion would be an autonumber field in your database as the file name, so reset filename to the db field value. Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
I was thinking that too... but I'm not certain as to how alter it, for I do not know what consequenses it would get.

BUT let's say that I search the db, finds the last Record, reads its ID value (autonumber, primary key) and adds 1 to it?!
How would I then change that line,
just replace LCase(..) with that value?

one other thing, thanks for guiding with me through this =) My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Prior to running the above code, create a new record in the database, if the field is set to autonumber all you need do is read the value
e.g. rs.addnew
then use something like this
poemid=rs(&quot;poemfilename&quot;) & &quot;.txt&quot;

then change
fileName = LCase(load.getFileName(&quot;file&quot;))
to
fileName = poemid

Should be as easy as that! (he says with a wry smile) Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
yeah, this simple code did it. but how the heck do i select the last recordset? Cause when using RS.MoveLast I get &quot;Rowset does not support fetching backward. &quot;
Could it have something to do with the fact that ID is a primary key?


Set RS = Conn.Execute(&quot;Select ID From poetry&quot;)
RS.MoveFirst
Do While not RS.EOF
poemid = RS(&quot;id&quot;) + 1
RS.MoveNext
Loop
RS.Close My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Dont understand why you're doing it that way, just make it an autonumber field, seems the easiest way to do it. When someone wants to upload a new file then just add a new record and immediately get the value of the autonumber field. No point doing a select all from the database and then looping thru til the end of the recordset.
For example

Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
Conn.Provider = &quot;Microsoft.Jet.OLEDB.4.0&quot;
Conn.ConnectionString = &quot;Data Source=&quot; & Server.MapPath (&quot;poems/poems.mdb&quot;)
Conn.Open
set rs= Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.addnew
poemnewfilename=rs(&quot;poemid&quot;)
'where poemid is an autonumber field in the database
Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top