Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    save file in db with unique name or id with asp classic

    I m new to asp my code is working absolutely fine, but i am facing two problems although these problems are not affecting the application. I just want to make good functionality to the application logically.

    1) when i upload excel file, and if another file is already present with the same name, then new file overwrites it..
    and if i manually change file name then file is saved with new name..
    i am trying to save this new file with unique name or new name,, i don’t know how to do this pls help..

    2) and second problem is that i want to save this file in db with the corresponding login user (for future reference).. i hope
    my problems will be solved. many thanks

    this is the url from which I got help..
    shotdev.com/asp/asp-excel/asp-upload-excel-import-to-database/

    code is pasted for review...


    Code:
    'upload_excel.asp               
    <form action="upload_excel_process.asp" method="post" enctype="multipart/form-data" name="frmMain" onSubmit="return checkData();">
    <input name="file1" type="file"> <input type="submit" name="Submit" value="Submit">
    <%mem_id=session("mem_id")%>
    <input type="hidden" name="client_id"  value="<%=mem_id%>">
    </form>
    process file

    Code:
    'upload_excel_process.asp
    		 
    <%client_id=session("mem_id")%>
    <%
    Dim xlApp,xlBook,xlSheet1,xlSheet2,OpenFile,i  
    Dim Conn,strSQL,client_id,objExec  
    Dim mySmartUpload  
    Dim sFileName	
    
    '*** Create Object ***'	
    Set mySmartUpload = Server.CreateObject("aspSmartUpload.SmartUpload")
    
    '*** Upload Files ***'
    mySmartUpload.Upload
    
    	'** Getfile Name ***'
    sFileName = mySmartUpload.Files("file1").FileName
    
    If sFileName <> "" Then
    
    mySmartUpload.Files("file1").SaveAs(Server.MapPath("excel/"&sFileName))
    
    '   my ref..\excel\" & session("mem_id") & "\" & now()
    
    OpenFile = "excel/"&sFileName
    
    '*** Create Exce.Application ***'
    Set xlApp = Server.CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Open(Server.MapPath(OpenFile))
    Set xlSheet1 = xlBook.Worksheets(1)		
    
    Set Conn = Server.Createobject("ADODB.Connection")
    Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("../db/database.mdb"),"" , ""
    For  i = 2 To 5  
    If Trim(xlSheet1.Cells.Item(i,1)) <> "" Then  
    strSQL = ""  
    strSQL = strSQL &"INSERT INTO add_contacts "  
    strSQL = strSQL &"(client_id,name_receiver,contact_person_receiver,street_receiver,city_receiver,tel_receiver,fax_receiver,country_receiver,zip_code_receiver) "  
    
    ' i have added fields to the db for file (file_name and file_id) 
    
    strSQL = strSQL &"VALUES "  
    strSQL = strSQL &"('"&client_id&"', '"&xlSheet1.Cells.Item(i,1)&"','"&xlSheet1.Cells.Item(i,2)&"','"&xlSheet1.Cells.Item(i,3)&"'"  
    strSQL = strSQL &",'"&xlSheet1.Cells.Item(i,4)&"','"&xlSheet1.Cells.Item(i,5)&"','"&xlSheet1.Cells.Item(i,6)&"','"&xlSheet1.Cells.Item(i,7)&"','"&xlSheet1.Cells.Item(i,8)&"')"
    
    Set objExec = Conn.Execute(strSQL)  
    Set objExec = Nothing  
    End IF  
    Next  
    
    
    
    xlApp.Application.Quit
    
    '*** Quit and Clear Object ***'
    Conn.Close()
    Set Conn = Nothing
    Set xlSheet1 = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing
    End If
    
    Set mySmartUpload = Nothing
    %>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    One way you could save with a unique name would be to add the date and time that the file was uploaded to the end of the name.

    Example:
    Code:
    sFileName = mySmartUpload.Files("file1").FileName
    
    when = Now()
    stamp = "-T" & ( Year(when) * 10000 + Month(when) * 100 + Day(when)  ) _
          & "-" & ( Hour(when) * 10000 + Minute(when) * 100 + Second(when) )
    
    sFileName = Replace( ".xls", stamp & ".xls" )
    And now just save using sFileName, as you are doing now.

    So a file uploaded as "test.xls" would end up with a name such as "test-T20120116-131512.xls" on the server.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •