Renaming a File

Discussion in 'ASP General' started by Keith, Jul 29, 2004.

  1. Keith

    Keith Guest

    Is it possible to rename a file which a user uploads to the server?

    I have a form which allows users to upload an image to a folder on the web
    server. This works fine.

    The form they submit takes their firstname, surname and the file.

    However, I want the file to automatically be renamed to
    firstname_surname.jpg (taking their firtname and surname from the form they
    are submitting).

    Is this possible?
     
    Keith, Jul 29, 2004
    #1
    1. Advertising

  2. Keith

    John Guest

    I use a the FREE upload component of www.dundas.com with full HELP on-line.


    Hereunder the ASP that rename the uploaded files to a random number

    <%
    Session("sender")="upload.asp"
    If Session("userId")="" then Response.redirect "login.asp"

    Response.Write "<html>"
    Response.Write "<header>"
    Response.Write "<title>Fotoserver HSW-MVA Upload</title>"
    %>
    <!--#Include file="common/conStr.inc"-->
    <%
    Response.Write "<link rel=""stylesheet"" type=""text/css""
    href=""common/css.inc"">"
    Response.Write "</header>"
    Response.Write "<body>"

    'On Error Resume Next

    Set MyConn=Server.CreateObject("ADODB.Connection")

    MyConn.Open Connect_String

    imagesPath = "upload"
    theDate = Right("00" & Month(date()),2) & "/" & Right("00" & Day(Date()),2)
    & "/" & Year(Date())

    Set objUpload = Server.CreateObject ("Dundas.Upload.2")

    objUpload.SaveToMemory

    fileName = Trim(objUpload.Form("ref"))
    credits = Ucase(Trim(objUpload.Form("credits")))
    fileNr = 0

    if fileName = "" then
    Response.Write "Ongeldige referentie."
    Else

    if objUpload.DirectoryExists(Server.MapPath(imagesPath & "\" &
    Session("userId") & "\" & fileName)) then
    objUpload.DirectoryDelete Server.MapPath(imagesPath & "\" &
    Session("userId") & "\" & fileName),1
    End if

    objUpload.DirectoryCreate Server.MapPath(imagesPath & "\" &
    Session("userId") & "\" & fileName)

    If Err.Number <> 0 Then

    Response.Write "Error " & Err.Number

    Else

    Response.Write "Aanbieder: " & Session("userCompany") & " / " &
    Session("userId") & "<br><br>"

    Response.Write "<table width=""760"" class=""small"" style=""border:#ffffff
    1 solid border"">"
    Response.Write "<tr><td width=""600"">Uploaded files</td><td
    width=""160"">Saved as</td></tr>"

    For Each objUploadedFile in objUpload.Files

    if Ucase(Right(objUploadedFile.OriginalPath,4))=".JPG" Or
    Ucase(Right(objUploadedFile.OriginalPath,4))=".GIF" then
    extention = Ucase(Right(objUploadedFile.OriginalPath,4))
    fileNr = fileNr + 1

    randomize
    nr1 = Clng(Rnd * 1000000)+1
    if nr1 > 1000000 then nr1 = 1000000
    if nr1 < 1 then nr = 1

    randomize
    nr2 = Clng(Rnd * 1000000)+1
    if nr2 > 1000000 then nr2 = 1000000
    if nr2 < 1 then nr = 1

    randomize
    nr3 = Clng(Rnd * 1000000)+1
    if nr3 > 1000000 then nr3 = 1000000
    if nr3 < 1 then nr = 1

    Response.Write "<tr><td width=""660"">" & objUploadedFile.OriginalPath &
    "</td><td width=""100"">" & Cstr(Nr1) & Cstr(Nr2) & Cstr(Nr3) & extention &
    "</td></tr>"

    'save to server
    objUploadedFile.SaveAs Server.MapPath(imagesPath & "/" & Session("userId")
    & "/" & fileName & "/" & Cstr(Nr1) & Cstr(Nr2) & Cstr(Nr3) & extention)
    End If
    Next

    Response.Write "</table>"


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set tf = fso.CreateTextFile(Server.MapPath(imagesPath & "/" &
    Session("userId") & "/" & fileName & "/default.asp"), True)
    tf.WriteLine("Oops")
    tf.Close



    'Update dbs (tblUploads)

    MyConn.Execute "DELETE * FROM tblUploads WHERE uploadId=" & _
    Chr(34) & Session("UserId") & Chr(34) & " AND upLoadRef=" & _
    Chr(34) & fileName & Chr(34)


    sql="INSERT INTO tblUploads
    (uploadId,uploadRef,uploadX,uploadDate,uploadTime) VALUES (" & _
    Chr(34) & Session("UserId") & Chr(34) & "," & _
    Chr(34) & fileName & Chr(34) & "," & _
    Cint(fileNr) & "," & _
    "#" & theDate & "#," & _
    "#" & time() & "#" & ")"

    MyConn.Execute sql
    Set MyConn=Nothing

    End If

    End if 'bad filename

    Set objUpload = Nothing
    Response.Write "</body></html>"

    'Response.Write "<a href=""upload.asp"">Volgende</a>"
    'Response.Redirect "upload.asp"



    %>
     
    John, Jul 29, 2004
    #2
    1. Advertising

  3. > Is it possible to rename a file which a user uploads to the server?

    Yes. Depending on the component you use for upload, you can do this during
    the upload (e.g. component.SaveAs(somefilename)) or use FileSystemObject
    after it's been uploaded. For the former, see the documentation for your
    upload component. For the latter, see:

    http://www.aspfaq.com/2074
    http://www.aspfaq.com/2039#rename

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)




    "Keith" <@.> wrote in message news:...
    >
    > I have a form which allows users to upload an image to a folder on the web
    > server. This works fine.
    >
    > The form they submit takes their firstname, surname and the file.
    >
    > However, I want the file to automatically be renamed to
    > firstname_surname.jpg (taking their firtname and surname from the form

    they
    > are submitting).
    >
    > Is this possible?
    >
    >
     
    Aaron [SQL Server MVP], Jul 29, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jamie Sutherland

    Renaming htm file to aspx

    Jamie Sutherland, Jan 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    356
    zPaul
    Jan 20, 2004
  2. Sergey Balan

    IE renaming file while download

    Sergey Balan, Mar 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    291
    Sergey Balan
    Mar 10, 2005
  3. Scott Steiner

    renaming file

    Scott Steiner, Jun 23, 2005, in forum: Java
    Replies:
    5
    Views:
    509
    Raymond DeCampo
    Jun 24, 2005
  4. Chris
    Replies:
    6
    Views:
    728
    Chris Uppal
    Mar 6, 2007
  5. Steven D'Aprano
    Replies:
    3
    Views:
    1,894
    Jeffrey Straszheim
    Dec 6, 2008
Loading...

Share This Page