Stefan Motz
asked on
Delete files with Classic ASP
He Experts,
I store PDF, Word, JPG, etc. files in a folder. Each of these files is linked from a web page.
I would like the users be able to delete the selected file by clicking a link in the record.
e.g. how can a file be deleted without hard coding the image location in the code?
Can it be doen like
myAttachment Request.QueryString("Attac hment")
then call the attachment in the code like
"\\myFiles\&myAttachment&"
I tried but it didn't work.
This is the code I'm using when the picture location is hard coded:
<%
dim fs
Set fs=Server.CreateObject("Sc ripting.Fi leSystemOb ject")
if fs.FileExists("\\myFiles\m yPic.jpg") then
fs.DeleteFile("\\myFiles\m yPic.jpg")
end if
set fs=nothing
%>
Thank you for your help.
I store PDF, Word, JPG, etc. files in a folder. Each of these files is linked from a web page.
I would like the users be able to delete the selected file by clicking a link in the record.
e.g. how can a file be deleted without hard coding the image location in the code?
Can it be doen like
myAttachment Request.QueryString("Attac
then call the attachment in the code like
"\\myFiles\&myAttachment&"
I tried but it didn't work.
This is the code I'm using when the picture location is hard coded:
<%
dim fs
Set fs=Server.CreateObject("Sc
if fs.FileExists("\\myFiles\m
fs.DeleteFile("\\myFiles\m
end if
set fs=nothing
%>
Thank you for your help.
ASKER
The name of the file is also stored in a database.
This is the link to the DeleteAttachment.asp
<a href="DeleteAttachment.asp ?ID=<%=rs( "ID")%>">D ELETE</a>
The code of the DeleteAttachment.asp is the following:
<%
dim fs
Set fs=Server.CreateObject("Sc ripting.Fi leSystemOb ject")
if fs.FileExists("\\Attachmen ts\EPS\Our Job.pdf") then
fs.DeleteFile("\\Attachmen ts\EPS\Our Job.pdf")
end if
set fs=nothing
myId = Request.QueryString("ID")
StrSQL = "Update HR_Letters SET Attachment= '' Where ID="&myId&""
conn.Execute StrSQL
'Redirect to the UPDATED page
Response.Redirect "ViewLetter.asp?key="&myId &""
%>
When I hard code the picture location it is working fine. It resets the Attachment field of the record to empty and it deletes the file.
How can I take the value of the Attachment field from the database (in this case 'OurJob.pdf') without hard coding it?
This is the link to the DeleteAttachment.asp
<a href="DeleteAttachment.asp
The code of the DeleteAttachment.asp is the following:
<%
dim fs
Set fs=Server.CreateObject("Sc
if fs.FileExists("\\Attachmen
fs.DeleteFile("\\Attachmen
end if
set fs=nothing
myId = Request.QueryString("ID")
StrSQL = "Update HR_Letters SET Attachment= '' Where ID="&myId&""
conn.Execute StrSQL
'Redirect to the UPDATED page
Response.Redirect "ViewLetter.asp?key="&myId
%>
When I hard code the picture location it is working fine. It resets the Attachment field of the record to empty and it deletes the file.
How can I take the value of the Attachment field from the database (in this case 'OurJob.pdf') without hard coding it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It gives me an error on line 7:
Microsoft OLE DB Provider for SQL Server error '80040e14'
Incorrect syntax near '='.
I tried to fix it but no success. The ID column is named ID in my database and it is INT data type
I find the records by ID,
The only change I made was renamint fileID to ID, so that it can find the record in my database
Microsoft OLE DB Provider for SQL Server error '80040e14'
Incorrect syntax near '='.
I tried to fix it but no success. The ID column is named ID in my database and it is INT data type
I find the records by ID,
The only change I made was renamint fileID to ID, so that it can find the record in my database
change
dim fileID : fileID = Request.QueryString("fileI D")
to
dim fileID : fileID = Request.QueryString("ID")
dim fileID : fileID = Request.QueryString("fileI
to
dim fileID : fileID = Request.QueryString("ID")
ASKER
Now it gives me this error:
Invalid column name 'fileID'.
Invalid column name 'fileID'.
ASKER
Thank you very much; it's working now.
ASKER
I really envy your brain Monty.
How can you always answer my question so quickly?
Thank you very much for your help.
How can you always answer my question so quickly?
Thank you very much for your help.
when you've been doing this as long as I have, it comes pretty easy :)
you always seem to catch me at work when it's slow!
you always seem to catch me at work when it's slow!
<%
dim fs
Set fs=Server.CreateObject("Sc
MyFile = Server.mappath("/folder/my
if fs.FileExists(MyFile) then
fs.DeleteFile(myfile)
end if
set fs=nothing
%>