If File Exists?

Nov 18, 2008 at 2:30 AM
I noticed that Flajaxian overwrites any file that already exists.  Is there a way to stop this happening and to add a suffix or something to the file being created? 

I'm needing this to work for both Amazon S3 and other server uploads.  Any tips/ideas?

Ta.  Steve
Coordinator
Nov 18, 2008 at 2:38 AM
<SCRIPT language="C#" runat="server">
protected void FileNameDetermining(object sender, FileNameDeterminingEventArgs args)
{
    
    if(this.FileExists(args.FileName)){
        args.FileName =
            Path.GetDirectoryName(args.FileName) +
            Path.DirectorySeparatorChar +             
            Path.GetFileNameWithoutExtension(args.FileName) + "(2)" +
            Path.GetExtension(args.FileName);
       // or if you want to stop the upload  call args.CancelProcessing = true;
   }
}
</SCRIPT>
<fjx:FileUploader ID="FileUploader1" runat="server">
    <Adapters>
        <fjx:FileSaverAdapter Runat="server" FolderName="UploadFolder" OnFileNameDetermining="FileNameDetermining" />
    </Adapters>
</fjx:FileUploader>
Jan 31, 2009 at 10:31 AM
Edited Jan 31, 2009 at 10:32 AM
this.FileExists() doen't work in my case because this refers to the current aspx page. My code:

 protected void FileNameDetermining(object sender, FileNameDeterminingEventArgs args)
        {
            string sPathAndFileName = Server.MapPath("UploadFolder") + "\\" + hfSessionNumber.Value + "\\" + args.FileName;

            if (File.Exists(sPathAndFileName))
            {
                args.FileName =
                    Path.GetDirectoryName(args.FileName) +
                    Path.DirectorySeparatorChar +
                    Path.GetFileNameWithoutExtension(args.FileName) + "(2)" +
                    Path.GetExtension(args.FileName);
                // or if you want to stop the upload  call args.CancelProcessing = true;
            }
            
        }

For me it makes sense but doesn't work

hfSessionNumber.Value refers to my current temp folder.

Coordinator
Feb 2, 2009 at 6:44 PM
"Doesn't work" means nothing to me. Do you get exception? Do you use Fiddler to detect HTTP error code? Do you not see file?


Feb 5, 2009 at 12:20 PM
Hi flajaxian, I see you have the code for this in CS, is it posible to get this in VB please.
I know you are busy and I have tried to convert it, but it didn't work for me.


Imports System.IO
Imports com.flajaxian

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub FileNameDetermining(ByVal sender As Object, ByVal args As FileNameDeterminingEventArgs)

        If Me.FileExists(args.FileName) Then
            args.FileName = (Path.GetDirectoryName(args.FileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(args.FileName) & "(2)") + Path.GetExtension(args.FileName)
            ' or if you want to stop the upload call args.CancelProcessing = true;
        End If
    End Sub
End Class

I am getting an error

<title>Compilation Error</title> <style> body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } </style>

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30456: 'FileExists' is not a member of '_Default'.

Source Error:

Line 6:      Protected Sub FileNameDetermining(ByVal sender As Object, ByVal args As FileNameDeterminingEventArgs)
Line 7:  
Line 8:          If Me.FileExists(args.FileName) Then
Line 9:              args.FileName = (Path.GetDirectoryName(args.FileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(args.FileName) & "(2)") + Path.GetExtension(args.FileName)
Line 10:             ' or if you want to stop the upload call args.CancelProcessing = true;


Thank you
Coordinator
Feb 5, 2009 at 3:48 PM
Edited Feb 5, 2009 at 3:48 PM
Well of course you don't have FileExists method defined in your class.

You can use that instead If System.IO.File.Exists(Server.MapPath(@"~/UploadFolder/" & args.FileName)) Then