Solved

unique name for each file uploaded

Posted on 2006-06-14
10
518 Views
Last Modified: 2012-06-27
At the moment i have this script which sends my file to a directory and adds information to the database. I also have a function that creates a random 10 digit number.
I need to give each file uploaded a unique name using the function, im not sure how to use the code (rndID(10)) which activates the function to save each file with a unique name and make sure that the same unique name is sent to the database. Can somebody help?

<!-- #include file="upload.asp" -->
<%
Function rndID(byVal maxLen)
     Dim strNewCode
     Dim whatsNext, upper, lower, intCounter
     Randomize
     For intCounter = 1 To maxLen
          whatsNext = Int((1 - 0 + 1) * Rnd + 0)
          If whatsNext = 0 Then
               upper = 90
               lower = 65
          Else  
               upper = 57
               lower = 48
          End If
          strNewCode = strNewCode & Chr(Int((upper - lower + 1) * Rnd + lower))
     Next
     rndID = strNewCode
end function


'Dimension variables
Dim adoCon              'Holds the Database Connection Object
Dim rs   'Holds the recordset for the new record to be added
Dim strSQL               'Holds the SQL query to query the database
Dim Uploader, File
Set Uploader = New FileUploader

' This starts the upload process
Uploader.Upload()

      ' Loop through the uploaded files
      For Each File In Uploader.Files.Items


      
                  ' Save the file
                  File.SaveToDisk "C:\Inetpub\wwwroot\flashsystem\flash\"

'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("flashdb.mdb")

'Create an ADO recordset object
Set rs = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT tblFlashinfo.Author, tblFlashinfo.Author_Link, tblFlashinfo.Title, tblFlashinfo.Entry_Type, tblFlashinfo.Height, tblFlashinfo.Width, tblFlashinfo.Description, tblFlashinfo.File_Name FROM tblFlashinfo;"

'Set the cursor type we are using so we can navigate through the recordset
rs.CursorType = 2

'Set the lock type so that the record is locked by ADO when it is updated
rs.LockType = 3

'Open the recordset with the SQL query
rs.Open strSQL, adoCon

'Tell the recordset we are adding a new record to it
rs.AddNew

'Add a new record to the recordset
rs.Fields("Author") =  Uploader.form("Author")
rs.Fields("Author_Link") = Uploader.form("Author_Link")
rs.Fields("Title") = Uploader.form("Title")
rs.Fields("Entry_Type") = Uploader.form("Entry_Type")
rs.Fields("Height") = Uploader.form("Height")
rs.Fields("Width") = Uploader.form("Width")
rs.Fields("Description") = Uploader.form("Description")
rs("file_Name") = File.FileName

'Write the updated recordset to the database
rs.Update            


'Reset server objects
rs.Close
Set rs = Nothing
Set adoCon = Nothing
      Next

Response.Redirect("submitted_flash.asp")
%>
0
Comment
Question by:cookiejest
  • 5
  • 3
  • 2
10 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Your uploader script has an option either to overwrite a file if it exists or to rename it. If you choose the second option, you will be set.

FtB
0
 

Author Comment

by:cookiejest
Comment Utility
Im not following you...

I changed my code so that the random function is only performed once and then the created value is given the dimension name of "NewName".

I have then changed the file name that is written to the database of the file name by changing

rs("file_Name") = File.FileName

to

rs("file_Name") = NewName + File.FileName

I am still having a problem however changing the file name itself when it is uploaded to the directory.

Why doesnt this work:

      ' Loop through the uploaded files
      For Each File In Uploader.Files.Items

NewName = rndID(10)

                  ' Save the file
                  File.SaveToDisk "C:\Inetpub\wwwroot\flashsystem\flash\" &  NewName&File.FileName

??


Here is the complete code:

<!-- #include file="upload.asp" -->
<%
Function rndID(byVal maxLen)
     Dim strNewCode
     Dim whatsNext, upper, lower, intCounter
     Randomize
     For intCounter = 1 To maxLen
          whatsNext = Int((1 - 0 + 1) * Rnd + 0)
          If whatsNext = 0 Then
               upper = 90
               lower = 65
          Else  
               upper = 57
               lower = 48
          End If
          strNewCode = strNewCode & Chr(Int((upper - lower + 1) * Rnd + lower))
     Next
     rndID = strNewCode
end function


'Dimension variables
Dim adoCon              'Holds the Database Connection Object
Dim rs   'Holds the recordset for the new record to be added
Dim strSQL               'Holds the SQL query to query the database
Dim NewName
Dim Uploader, File
Set Uploader = New FileUploader
' This starts the upload process
Uploader.Upload()

      ' Loop through the uploaded files
      For Each File In Uploader.Files.Items

NewName = rndID(10)

                  ' Save the file
                  File.SaveToDisk "C:\Inetpub\wwwroot\flashsystem\flash\" &  NewName&File.FileName

'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("flashdb.mdb")

'Create an ADO recordset object
Set rs = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT tblFlashinfo.Author, tblFlashinfo.Author_Link, tblFlashinfo.Title, tblFlashinfo.Entry_Type, tblFlashinfo.Height, tblFlashinfo.Width, tblFlashinfo.Description, tblFlashinfo.File_Name FROM tblFlashinfo;"

'Set the cursor type we are using so we can navigate through the recordset
rs.CursorType = 2

'Set the lock type so that the record is locked by ADO when it is updated
rs.LockType = 3

'Open the recordset with the SQL query
rs.Open strSQL, adoCon

'Tell the recordset we are adding a new record to it
rs.AddNew

'Add a new record to the recordset
rs.Fields("Author") =  Uploader.form("Author")
rs.Fields("Author_Link") = Uploader.form("Author_Link")
rs.Fields("Title") = Uploader.form("Title")
rs.Fields("Entry_Type") = Uploader.form("Entry_Type")
rs.Fields("Height") = Uploader.form("Height")
rs.Fields("Width") = Uploader.form("Width")
rs.Fields("Description") = Uploader.form("Description")
rs("file_Name") = NewName + File.FileName

'Write the updated recordset to the database
rs.Update            


'Reset server objects
rs.Close
Set rs = Nothing
Set adoCon = Nothing
Set NewName = nothing
      Next

Response.Redirect("submitted_flash.asp")
%>
0
 
LVL 25

Expert Comment

by:kevp75
Comment Utility
are you sure the files aren't getting uploaded  (check the directory you are uploading to)

i noticed you have this    NewName + File.FileName
where you are trying to insert the data into the table....it should be
NewName & File.FileName

if the files are there your uploader is doing what it is supposed to, if not make sure to check NTFS permissions on the folder and make sure IUSR_machinename has read/write/modify access to it
0
 

Author Comment

by:cookiejest
Comment Utility
Yeh i can upload files and form data fine with this script but im trying to make sure each file is saved with a unique name and the same unique name is sent to the database.

is this line definatly correct?

  File.SaveToDisk "C:\Inetpub\wwwroot\flashsystem\flash\" &  NewName&File.FileName
0
 
LVL 25

Expert Comment

by:kevp75
Comment Utility
yes, but only if the uploader class supports it.  Alot of components have a SaveToDisk, that will just save the file(s) as they are uploaded with the original name, and another method like .SaveAs which allows you to specify the name of the file

your best bet may be to set the filename first in a variable
something like this

strFileName = NewName & File.FileName
File.SaveToDisk "C:\Inetpub\wwwroot\flashsystem\flash\" & strFileName


is this the PureASPUpload class?
gimme just a few minutes to look through it and see if they have the option of allowing a SaveAs method
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 25

Expert Comment

by:kevp75
Comment Utility
yeah, I see the PureASPUpload class is pretty limited to what it can do, the class itself does not look like it can handle renaming a file on upload.


any chance we can talk you into using another uploading class that will?

here's the one that I have used, with great success:
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=8525&lngWId=4
0
 

Author Comment

by:cookiejest
Comment Utility
will my code have to change much?
0
 
LVL 25

Expert Comment

by:kevp75
Comment Utility
you will have to look at the link I posted to answer that.

you won't have to change your database stuff, but the way you initiate the upload is different, please see the link I posted for instructions
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 300 total points
Comment Utility
>>Im not following you...<<

The idea is that if your script supports the no-overwrite function, it will always give the file a unique name.

As an aside, random is good but not foolproof.

If you want something that will truly be unique, try this:

Function createGuid()
  Set objTypeLib = Server.CreateObject("Scriptlet.TypeLib")
  strGUID = objTypeLib.Guid
  Set objTypeLib = Nothing
  createGuid =  strGUID
End Function

FtB
0
 
LVL 25

Expert Comment

by:kevp75
Comment Utility
i'm confused?

1st off is the C grade, 2nd off if this solution provided the answer then why not a split, as they are both viable solutions?  (even though it is probably not the correct solution as the PureASPUpload class does not allow renaming an uploaded file)

not that I hold grudges, but I now know who's questions to stay away from...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now