SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Zealot gidday's Avatar
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi asad_ahmed

    Saving the the file with a unique name shouldn't be too hard. There are numerous ways of doing this, it just depends on how clever you want to be. It could be as easy as appending something unique to the end of the file name eg [NOW()]. Or you could use FileSystemObject to see if the file already exists which then lets you decide what to do, or you could simply rename all uploaded files saving the last file name for recall for the next eg save ThisFileName1 and the next will be ThisFileName2.

    Now as to your second question. Is it working? If not, are you getting an error? You need to help us.
    Joe
    "All that is necessary for the triumph of evil
    is that good men do nothing."

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Quote Originally Posted by gidday View Post

    Thankyou so much,,
    i am not getting any error because i m not saving files right now.... i didn't add this functionality yet,,
    but i want to do so,,
    so thats why i m looking for expert advice..

    pls tell me the code for my questions bcuz i dont now how to solve these issues
    many thanks,,,,

  4. #4
    SitePoint Zealot gidday's Avatar
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi asad_ahmed

    Sorry, that's not the way I work. I assist but I don't do your work for you. The forums are here for your support. You need to help us so we can help you.
    Joe
    "All that is necessary for the triumph of evil
    is that good men do nothing."

  5. #5
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Hello gidday

    thanks for reply,,i hope you didn't mind


Tags for this Thread

Bookmarks

Posting Permissions

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