File deletion from server folder

G

Guest

I'm building a site using VS 2005, Expression, vb.net & asp.net 2.0

I want people to be able to upload text & photos - Accomplished!
I want people to be able to edit / delete text on the database - Acomplished!
I want people to be able to delete their uploaded photos from the file
system - Problem!

How do I go about deleting the photo someone has uploaded to a folder?
Say the actual file on the server is: ~/images/picture1.jpg

What i want to do is when someone deletes their entry on the database the
associated photo is also deleted from the file system.
(The database holds the image url in the following format:
~/images/imagename.jpg)

Any pointers would greatly appreciated!
 
S

Steve C. Orr [MCSD, MVP, CSM, ASP Insider]

You didn't actually specify what your problem is.
Obviously you'd use the System.IO.File.Delete method.
I bet your problem is with permissions though.
You may need to adjust the security on that folder to allow the ASPNET (or
NetworkService) user delete permissions.
Alternately you could use impersonation to have ASP.NET run under a
different user account:
<!-- Web.config file. -->
<identity impersonate="true" userName="Redmond\BillG" password="Melinda"/>
 
G

Guest

Thanks Steve

I'll play around with the system.IO.file.delete method and permissions and
see what i come up with.

If I get in to problems I'll re-post.

Thanks again

JP
 
G

Guest

How do I go about deleting the photo someone has uploaded to a folder?
Say the actual file on the server is: ~/images/picture1.jpg

string filename = "~/images/picture1.jpg";
File.Delete(Server.MapPath(filename));
 
G

Guest

Thanks Alexey

Could you perhaps show me the context of your code say in a buttons click
event?

Cheers

James
 
G

Guest

Thanks Alexey

Could you perhaps show me the context of your code say in a buttons click
event?

I think it can be similar to the following:

void DeleteButton_Click(object sender, EventArgs e)
{
//someone deletes their entry on the database
//let us assume that you have a userid UID
//find picture name

string sql1="SELECT picture FROM users WHERE userid=" +
UID.ToString();
.....

string filename = ... // result of sql-query
File.Delete(Server.MapPath(filename));

//delete user record
string sql2="DELETE FROM users WHERE userid=" + UID.ToString();
.....
}

File is from the System.IO
 
J

James Page

Alexey said:
I think it can be similar to the following:

void DeleteButton_Click(object sender, EventArgs e)
{
//someone deletes their entry on the database
//let us assume that you have a userid UID
//find picture name

string sql1="SELECT picture FROM users WHERE userid=" +
UID.ToString();
....

string filename = ... // result of sql-query
File.Delete(Server.MapPath(filename));

//delete user record
string sql2="DELETE FROM users WHERE userid=" + UID.ToString();
....
}

File is from the System.IO
Thanks again Alexey

Do you have a VB.net example?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top