473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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...


Expand|Select|Wrap|Line Numbers
  1. 'upload_excel.asp               
  2. <form action="upload_excel_process.asp" method="post" enctype="multipart/form-data" name="frmMain" onSubmit="return checkData();">
  3. <input name="file1" type="file"> <input type="submit" name="Submit" value="Submit">
  4. <%mem_id=session("mem_id")%>
  5. <input type="hidden" name="client_id"  value="<%=mem_id%>">
  6. </form>
  7.  
process file

Expand|Select|Wrap|Line Numbers
  1. 'upload_excel_process.asp
  2.  
  3. <%client_id=session("mem_id")%>
  4. <%
  5. Dim xlApp,xlBook,xlSheet1,xlSheet2,OpenFile,i  
  6. Dim Conn,strSQL,client_id,objExec  
  7. Dim mySmartUpload  
  8. Dim sFileName    
  9.  
  10. '*** Create Object ***'    
  11. Set mySmartUpload = Server.CreateObject("aspSmartUpload.SmartUpload")
  12.  
  13. '*** Upload Files ***'
  14. mySmartUpload.Upload
  15.  
  16.     '** Getfile Name ***'
  17. sFileName = mySmartUpload.Files("file1").FileName
  18.  
  19. If sFileName <> "" Then
  20.  
  21. mySmartUpload.Files("file1").SaveAs(Server.MapPath("excel/"&sFileName))
  22.  
  23. '   my ref..\excel\" & session("mem_id") & "\" & now()
  24.  
  25. OpenFile = "excel/"&sFileName
  26.  
  27. '*** Create Exce.Application ***'
  28. Set xlApp = Server.CreateObject("Excel.Application")
  29. Set xlBook = xlApp.Workbooks.Open(Server.MapPath(OpenFile))
  30. Set xlSheet1 = xlBook.Worksheets(1)        
  31.  
  32. Set Conn = Server.Createobject("ADODB.Connection")
  33. Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("../db/database.mdb"),"" , ""
  34. For  i = 2 To 5  
  35. If Trim(xlSheet1.Cells.Item(i,1)) <> "" Then  
  36. strSQL = ""  
  37. strSQL = strSQL &"INSERT INTO add_contacts "  
  38. strSQL = strSQL &"(client_id,name_receiver,contact_person_receiver,street_receiver,city_receiver,tel_receiver,fax_receiver,country_receiver,zip_code_receiver) "  
  39.  
  40. ' i have added fields to the db for file (file_name and file_id) 
  41.  
  42. strSQL = strSQL &"VALUES "  
  43. strSQL = strSQL &"('"&client_id&"', '"&xlSheet1.Cells.Item(i,1)&"','"&xlSheet1.Cells.Item(i,2)&"','"&xlSheet1.Cells.Item(i,3)&"'"  
  44. 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)&"')"
  45.  
  46. Set objExec = Conn.Execute(strSQL)  
  47. Set objExec = Nothing  
  48. End IF  
  49. Next  
  50.  
  51.  
  52.  
  53. xlApp.Application.Quit
  54.  
  55. '*** Quit and Clear Object ***'
  56. Conn.Close()
  57. Set Conn = Nothing
  58. Set xlSheet1 = Nothing
  59. Set xlBook = Nothing
  60. Set xlApp = Nothing
  61. End If
  62.  
  63. Set mySmartUpload = Nothing
  64. %>
  65.  
Jan 16 '12 #1
3 3391
C CSR
144 100+
I believe the original filename is stored in your var sFileName, and I believe its id is "File1", so you'd have to rename it.
See if you can do this in your SaveAs(Server.MapPath(...)). Instead of "excel1/" & sFilename, use "excel1/" & "newname" & "." & "[whatever you want the extension to be]".

I believe if you used some concatenation and incrementation with something like a trailing number for each time you upload, the filename would be changed (e.g.; from "MyFile1.xlsx" to "MyFile2.xlsx", etc., etc.).

Does that make sense? There's alot of things you could do like that.

I can't test it right now, but let me know if it'll work.

As far as saving it in the database, do you mean the content or just the filename as a reference?
Jan 17 '12 #2
thanks for your reply
it is very helpfull for me.
i just want to save the filename as a reference for future reference.
Jan 18 '12 #3
C CSR
144 100+
While your saving the upload to a folder also stick in a few more lines of code beneath there to open your table of preference and stick the name you just used into a field somewhere in a database table. Then in the future, before you upload the file, you could reference the oldname out of the table, manipulate the name some way as I descibed before and save the file to folder and filename back to the table, probably in a new record if you want to maintain a list of these files for future reference. If you aren't familiar with doing something I've mentioned here, reply and I'll walk you through some basic process for getting this done. Just have a good idea ready for renaming the file, where in the database you want to store it (tablename, fieldname, etc.) and we can go from there. Good luck.
Jan 18 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sebastian Kerekes | last post by:
Greetings, I'm new to XSD and I'm trying to create a schema for the following: The document 'persons' should contain 0+ elements of type 'person', which has a subelement called 'name' which is a...
9
by: Ivan Demkovitch | last post by:
Hi! I would like to know if I can save File on Server using server-side code? For example, I like to create thumbnail images and populate specific directory. Do I need specific permissions...
6
by: Mike | last post by:
can i open the save file dialog box from a asp.net web page? thx
3
by: MilanB | last post by:
Hello I'm asking is it possible that two or more Web User Control have same file (.ascx) name within same project? Namespace is different. I got error message: "CS1595: 'ASP.EditForm_ascx' is...
1
by: Jameel | last post by:
Hello, uptill now i able to save the files to the server with thier original names, but i want to save the files with different names to avoid duplication of file names. see the following for what...
5
by: deko | last post by:
Is there an accepted or standard way to get a unique name given a string and the collection in which it needs to be unique? Should I use a HashTable? Other options? Here's a first crack: ...
4
by: Jonny | last post by:
Hello Group How do I open a Save File Dialog from an ASPX page behind a browse button? Any help would be fantastic!! I am using ASP.NET 1.1 using VB.NET as the coding language TIA
3
by: mse07 | last post by:
hi for every one i want to save text file in specified bath without show the save window i write code that save file but that code show save window : CommonDialog1.FileName =...
2
by: AccessHunter | last post by:
Hi, In my VBA code I am saving an excel spreadsheet with data from a table. I would like to prompt the Save File or Save As Dialog box with a default File Name, after opening the spreadsheet. ...
2
by: simonyong | last post by:
Hello, anyone I had search for few days with how to save file when user choose a file name from listbox and i will search the file from database and user can save it into their desktop what I had...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.