We help IT Professionals succeed at work.

unique name for each file uploaded

cookiejest
cookiejest asked
on
537 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")
%>
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2005

Commented:
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

Author

Commented:
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")
%>

Commented:
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

Author

Commented:
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

Commented:
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

Commented:
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

Author

Commented:
will my code have to change much?

Commented:
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
CERTIFIED EXPERT
Top Expert 2005
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
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...
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.