Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2011, 05:34 PM
Authorized User
 
Join Date: Jan 2011
Posts: 89
Thanks: 1
Thanked 0 Times in 0 Posts
Default Delete image file

I'm storing profile pictures in a folder ~/Account/Pictures/... and can save new pictures and update the URL in the database but I'm stuck on how to remove the old one.

Could it be that I'm passing the File.Delete() method a string like: ~/Account/Pictures/b9b57a75-2287-4e8d-8fc9-8c8cc6e5ef5a.jpg

I'm using this code:

Code:
Dim virtualFolder As String = "~/Account/Pictures/"
            Dim physicalFolder As String = Server.MapPath(virtualFolder)
            Dim conString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
            Dim con As New SqlConnection(conString)

            ' save new picture
            Dim picName As String = Guid.NewGuid().ToString()
            Dim extension As String = Path.GetExtension(PictureUrl.FileName).ToString()
            PictureUrl.SaveAs(Path.Combine(physicalFolder, picName + extension))

            ' delete old picture
            Dim cmdSelect As SqlCommand = New SqlCommand("SELECT [PictureUrl] FROM [UserDetails] WHERE ([UserName] = @UserName)")
            cmdSelect.Parameters.AddWithValue("@UserName", CurrentUser)
            con.Open()
            Dim oldPic As String = cmdSelect.ExecuteScalar()
            File.Delete(oldPic)

            ' update new PictureUrl in DB
            Dim cmdUpdate As SqlCommand = New SqlCommand("UPDATE [UserDetails] SET [PictureUrl] = @PictureUrl WHERE [UserName] = @UserName", con)
            cmdUpdate.Parameters.AddWithValue("@UserName", CurrentUser)
            cmdUpdate.Parameters.AddWithValue("@PictureUrl", virtualFolder + picName + extension)
            'con.Open()
            cmdUpdate.ExecuteNonQuery()
            con.Close()
            con.Dispose()
 
Old April 16th, 2011, 12:28 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

You just need to make sure you capture the URL to the old image before you delete it. If you can "find" an image via it's URL in the database, then you should be able to use that path to delete the image. Whether you delete the old image before or after updating the URL in the database is up to you. In most cases it shouldn't matter. However, on the off chance that you can't complete the entire procedure, changing the URL before deleting the old image could leave old, unused images lying around, while deleting the image first could leave you with URLs that have not been updated even though you already deleted the images they point to. Since that would leave you with users looking at red Xs instead of their new images, I would probably change the URL first. It's easy enough to maintain outdated images in the background where no user will ever notice. You could even build a script that hunts down any old image in your file structure that doesn't have a corresponding path in the database. There's also probably a pretty simple way from a server admin perspective to be able to tell when the last time a file was downloaded and / or run a report of all images sorted by their last download date. Then you can personally decide how long you're willing to let files "linger" on the server before you delete them.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old April 16th, 2011, 04:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Originally Posted by chroniclemaster1 View Post
You just need to make sure you capture the URL to the old image before you delete it.
I think he's already doing that. This code uploads and saves the new image (which means it's already there), finds the URL of the old image, deletes the old image and then saves the URL of the new image in the database. There's about a nano-second time window where the old image no longer exists and before the new image is stored in the database which seems acceptable to me (read on if it's not acceptable for you).

But anyways, the problem lies in this comment:

Quote:
Could it be that I'm passing the File.Delete() method a string like: ~/Account/Pictures/b9b57a75-2287-4e8d-8fc9-8c8cc6e5ef5a.jpg
Yes, exactly. ~/Account... is a virtual path which makes sense in a Web context. However, File.Delete works anywhere in the .NET Framework and as such needs a physical path. You can convert to one using Server.MapPath (just as you do with the Uploads folder when you save the image):

Code:
 
Dim oldPic As String = Server.MapPath(cmdSelect.ExecuteScalar())
File.Delete(oldPic)
Hope this helps,

Imar

P.S. On the off chance that updating the database fails you may want to delete the image after you saved the new URL. This way, you won't accidentally delete an image that is still in use. E.g.:

Code:
 
Dim oldPic As String = cmdSelect.ExecuteScalar()
 
Dim cmdUpdate As SqlCommand = New SqlCommand("UPDATE [UserDetails] SET [PictureUrl] = @PictureUrl WHERE [UserName] = @UserName", con)
...
cmdUpdate.ExecuteNonQuery()
...
File.Delete(oldPic)
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
07Raghav (June 16th, 2013)
 
Old April 16th, 2011, 07:42 AM
Authorized User
 
Join Date: Jan 2011
Posts: 89
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks, should have noticed since I was using Server.MapPath for saving.

I'm trying to stop if from deleting admin.jpg or default.jpg if that is the current users picture when changing it:

Code:
' check pic isnt admin or default
            If Not oldPicVirtual = "~/Account/Pictures/admin.jpg" OrElse Not oldPicVirtual = "~/Account/Pictures/default.jpg" OrElse Not oldPicVirtual = "" Then
                Dim oldPic As String = Server.MapPath(oldPicVirtual)
                File.Delete(oldPic)
            End If
But wierdly the file is still being deleted. While debuggin oldPicVirtual = "~/Account/Pictures/default.jpg" but the if block still executes??? I think something silly is happening here...
 
Old April 16th, 2011, 08:13 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You got your Or logic mixed up. You have this:

Code:
 
If it's not Admin Or it's not Default.
When it's Admin, it's not Default and vice versa. This means your code block always runs ;-)

I think you want And instead:

Code:
 
If it's not Admin And it's not Default.
With And, all conditions need to be true. E.g. it can't be Admin and it can't be Default. If one of them matches, the If block won't run:

Code:
 
If Not oldPicVirtual = "~/Account/Pictures/admin.jpg" And Not oldPicVirtual = "~/Account/Pictures/default.jpg" And Not oldPicVirtual = "" Then
  Dim oldPic As String = Server.MapPath(oldPicVirtual)
  File.Delete(oldPic)
End If
Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 16th, 2011 at 08:15 AM..
 
Old April 16th, 2011, 08:28 AM
Authorized User
 
Join Date: Jan 2011
Posts: 89
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Oh man, I knew it was something silly. Thanks.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to delete file System.IO.Delete error maricar C# 13 March 14th, 2014 06:50 AM
Delete image from Folder phuc2583 ASP.NET 3.5 Professionals 2 May 31st, 2010 10:20 PM
Image -- Insert/Update/Delete in sql2000 !!! dagad ASP.NET 2.0 Professional 0 October 27th, 2006 08:25 AM
Image -- Insert/Update/Delete in sql2000 !!! dagad ASP.NET 2.0 Basics 0 October 27th, 2006 08:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.