locked
i have problem in my codes :) upload picture file RRS feed

  • Question

  • User-1418772296 posted

    hello friends

    my codes are below.. when i upload tree.jpg first, tree.jpg is saved as 0tree.jpg... but when i upload tree.jpg second, tree.jpg is saved as 0tree.jpg again.. but it should be save as 1tree.jpg secondly

    where s problem ?

    cheers

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

    c = 0

    Dim xxx As Label

    xxx:

    Dim DestinationPath As String

    DestinationPath = "~\resimler\" & c.ToString & FileUpload1.FileName

    If System.IO.File.Exists(DestinationPath) = True Then

    c = c + 1

    GoTo xxx

    End If

    If FileUpload1.FileName <> "" Then

    Dim ext As String = Path.GetExtension(DestinationPath)

    If ext.ToLower() = ".jpg" Or ext.ToLower() = ".jpeg" Or ext.ToLower() = ".bmp" Or ext.ToLower() = ".png" Or ext.ToLower() = ".tif" Or ext.ToLower() = ".gif" Then

    If Image1.ImageUrl = "" Then

    FileUpload1.PostedFile.SaveAs(Server.MapPath(DestinationPath))

    Image1.ImageUrl = DestinationPath

    Friday, June 6, 2008 8:44 AM

Answers

  • User1582995914 posted

    Hi,

    Your code is almost right but you have assigned

    "C=0"

    inside button click so each time you click the button c is traeted as 0,

    so assign C=0 while declaration itself.

     

    Regards,

    Ashokkumar. T.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 6, 2008 9:53 AM
  • User-1418772296 posted

    hello friends

    c = 0 yes i write to this but c=c+1 below and go to xxx and xxx below of c=0.. why does c be 1 or 2 ??

    Dim xxx As Label

    xxx:

    Dim DestinationPath As String DestinationPath = "~\resimler\" & c.ToString & FileUpload1.FileName

    If System.IO.File.Exists(DestinationPath) = True Then

    c = c + 1

    GoTo xxx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 6, 2008 11:38 AM
  • User-1880918579 posted

    Looks like the "loop" you've got should do what you want it to. There is not anything wrong with using goto intelegently but i prefer using looping structures for simple things. Also closely examinee your results in DestinationPath with the debugger to make sure it really exists. I also don't know what happens when you DIM the "DestinationPath" inside that type of loop.

     I would change this:

     

    c = 0
    
    Dim xxx As Label 
    
    xxx:
    
    Dim DestinationPath As String
    
    DestinationPath = "~\resimler\" & c.ToString & FileUpload1.FileName 
    
    If System.IO.File.Exists(DestinationPath) = True Then
    
    c = c + 1
    
    GoTo xxx 
    
    End If
    

     

    to this:

    c = 0
    while System.IO.File.Exists("~\resimler\" & convert.tostring(c) & FileUpload1.FileName)
    c = c + 1
    end while
    DestinationPath = "~\resimler\" & convert.tostring(c) & FileUpload1.FileName

    This is the same but simpler:

    c = 0 

    DestinationPath = "~\resimler\" & convert.tostring(c) & FileUpload1.FileName

    while System.IO.File.Exists(DestinationPath)

    c = c + 1

    DestinationPath = "~\resimler\" & convert.tostring(c) & FileUpload1.FileName

    end while

     

    I typed both of those peices of code straight to the browser so there may be some minor errors. Hopefully you get the idea.

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 10, 2008 10:46 AM

All replies

  • User1582995914 posted

    Hi,

    Your code is almost right but you have assigned

    "C=0"

    inside button click so each time you click the button c is traeted as 0,

    so assign C=0 while declaration itself.

     

    Regards,

    Ashokkumar. T.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 6, 2008 9:53 AM
  • User-1418772296 posted

    hello friends

    c = 0 yes i write to this but c=c+1 below and go to xxx and xxx below of c=0.. why does c be 1 or 2 ??

    Dim xxx As Label

    xxx:

    Dim DestinationPath As String DestinationPath = "~\resimler\" & c.ToString & FileUpload1.FileName

    If System.IO.File.Exists(DestinationPath) = True Then

    c = c + 1

    GoTo xxx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 6, 2008 11:38 AM
  • User-1880918579 posted

    Looks like the "loop" you've got should do what you want it to. There is not anything wrong with using goto intelegently but i prefer using looping structures for simple things. Also closely examinee your results in DestinationPath with the debugger to make sure it really exists. I also don't know what happens when you DIM the "DestinationPath" inside that type of loop.

     I would change this:

     

    c = 0
    
    Dim xxx As Label 
    
    xxx:
    
    Dim DestinationPath As String
    
    DestinationPath = "~\resimler\" & c.ToString & FileUpload1.FileName 
    
    If System.IO.File.Exists(DestinationPath) = True Then
    
    c = c + 1
    
    GoTo xxx 
    
    End If
    

     

    to this:

    c = 0
    while System.IO.File.Exists("~\resimler\" & convert.tostring(c) & FileUpload1.FileName)
    c = c + 1
    end while
    DestinationPath = "~\resimler\" & convert.tostring(c) & FileUpload1.FileName

    This is the same but simpler:

    c = 0 

    DestinationPath = "~\resimler\" & convert.tostring(c) & FileUpload1.FileName

    while System.IO.File.Exists(DestinationPath)

    c = c + 1

    DestinationPath = "~\resimler\" & convert.tostring(c) & FileUpload1.FileName

    end while

     

    I typed both of those peices of code straight to the browser so there may be some minor errors. Hopefully you get the idea.

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 10, 2008 10:46 AM