Solved

unique name for each file uploaded

Posted on 2006-06-14
10
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 16906829
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
ID: 16907239
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
ID: 16907641
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:cookiejest
ID: 16909306
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
ID: 16910517
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
 
LVL 25

Expert Comment

by:kevp75
ID: 16910546
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
ID: 16911076
will my code have to change much?
0
 
LVL 25

Expert Comment

by:kevp75
ID: 16911279
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
ID: 16911480
>>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
ID: 16915957
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Save ms data to server side. 19 64
Adding 1 the value of input "text" every 3 seconds 5 48
Boolean 13 50
Post from Jquery mobile form goes wrong in classic asp action page 2 37
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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