How to get File Path in FileManagerUploading event?
Question asked by Nitin Patil - December 8, 2014 at 1:34 PM
Answered
Hi,
 
I am trying to get file path while uploading a file. In FileManagerUploading event I can get file name only, but not able to get full file path.
 
Can you please help?
 
Thanks,
Nitin

16 Replies

Reply to Thread
0
Cem Alacayir Replied
Employee Post
Hi,
You can do it like this:
 
private static void FileManagerUploading(object sender, FileManagerUploadingEventArgs e)
{
    foreach (var validation in e.Validations)
    {
        var filePath = System.IO.Path.Combine(e.Folder.PhysicalPath, validation.Name);
    }
}
 
0
Nitin Patil Replied
Thanks Cem,

The solution you provided, returns file path combined with Root Folder location. But the file would not be available at root folder until upload get finished. (FileManagerUploading event).

While uploading new file using “Fileultimate” control, I would like to check if the file is already present in my application. I will check file duplicity using file properties (like, file Size). That’s the reason, I am looking for full file path so that I can create a File object using System.IO and perform check duplicate operation before a file get uploaded to root folder location.

Please advise.

Thanks,
Nitin
0
Cem Alacayir Replied
Employee Post
In FileManagerUploading event, the file is not received yet, that's why you don't have any file or file stream at this point. However some useful properties like validation.Size and validation.ContentType are already available and you can use those and also filePath to check duplicity.
0
Steve Replied
Cem,
 
I just updated to version FileUltimate v4.3.0.2 and noticed the e.Folder.PhysicalPath property is no longer availabile.  I am using this property in all of the FileUltimate events for logging purposes.  I assume the new method uses the AlphaFS library.  How can I get the windows system path? I need it in the same format that I used for the RootFolder.Location (ie: \\server\share)
var filePath = System.IO.Path.Combine(e.Folder.PhysicalPath, validation.Name)
Thanks,
Steve
0
Cem Alacayir Replied
Employee Post
Hi Steve,
Yes, PhysicalPath had to disappear because we abstracted the file system. For example, the passed folder may be in a database in the future so PhysicalPath is meaningless for that kind of file system.
However it’s still possible to find out that value considering you already know you are using a physical file system. Just cast it like this:
 
string physicalPath;
using (var fileSystem = (AlphaPhysicalFileSystem)folder.GetFileSystem())
    physicalPath = fileSystem.PhysicalPath;
The using block is required because IFileSystem is disposable, for example if you specified username & password for RootFolder.Location, impersonation will happen and the file system should be disposed to rollback impersonation. If you don't use credentials for your root folders, then you can use this simpler version:
 
var physicalPath = ((AlphaPhysicalFileSystem)folder.GetFileSystem()).PhysicalPath
Note that in partial-trust hosting, you would need to cast to PhysicalFileSystem instead of AlphaPhysicalFileSystem because in that case AlphaFS (long path support) will not be available and regular Sytem.IO will be used.
 
We are still improving this new API, so some minor changes can be made again. Maybe we can add PhysicalPath property back and return non-empty string only when it's possible for easier access.
0
Steve Replied
Thank you so much!

I have been a customer since the FileVista days. I have been impressed with your level of service in the forums over the years.

Thanks again for standing my by your product.
0
Cem Alacayir Replied
Employee Post
You are welcome, thanks for the feedback.
0
Cem Alacayir Replied
Employee Post Marked As Answer
FYI, as of latest version (v4.4), you can again easily access the physical path like this in your event handler:
 
e.FileSystem.PhysicalPath
 
The FileSystem property of the event is new and it represents the already opened file system. So you no longer need to call e.folder.GetFileSystem() method and you no longer need to cast to a specific file system.
0
Chris Bookout Replied
Can you give an example of how to get the filename. I only get the path when using e.FileSystem.PhysicalPath.FileName?
0
Cem Alacayir Replied
Employee Post
If you are asking about FileManagerUploading event, you should iterate through e.Validations collection: foreach (var validation in e.Validations) { validation.Name; } .This is because multiple files can be uploaded at once so event there is one file it will be available in e.Validations collection.
0
Chris Bookout Replied
Is there a way to access it from the code behind? I need to email a list of the files uploaded or downloaded.
0
Cem Alacayir Replied
Employee Post
Yes the actual point of this post is accessing event data from code behind. Please see "Events" sample in the example project to see how you register event handlers in code behind and how you then access the event data and do something with it, e.g. sending an email. Your event handlers should be static because they will be called out of the context of your initial aspx page.
0
Chris Bookout Replied
Thanks for all the help. It still does not recognize e.validations though.
0
Cem Alacayir Replied
Employee Post
Sorry, the original question was about FileManagerUploading event which is a "before" event and e.Validations property is available for that event. If you are talking about FileManagerUploaded event which is an "after" event, then you should look for e.Items property instead. In "Events" sample (Events.aspx.cs), all events and their properties are demonstrated. By the way don't you have Intellisense enabled in Visual Studio which shows all available properties when you type "e."?
0
Chris Bookout Replied
Yes. Thanks again.
0
Chris Bookout Replied
Thanks again, It is working.

Reply to Thread