开发者

A problem with Relative Path Resolution when setting an Image Source

开发者 https://www.devze.com 2022-12-15 14:23 出处:网络
I have built a small WPF application that allows users to upload documents and then select one to display.

I have built a small WPF application that allows users to upload documents and then select one to display.

The following is the code for the file copy.

    
public static void MoveFile( string directory, string subdirectory)
{
    var open = new OpenFileDialog {Multiselect = false, Filter = "AllFiles|*.*"};
    var newLocation = CreateNewDirectory( directory, subdirectory, open.FileName);

    if ((bool) open.ShowDialog())
        CopyFile(open.FileName, newLocation);
    else
        "You must select a file to upload".Show();
}开发者_运维技巧

private static void CopyFile( string oldPath, string newPath)
{
 if(!File.Exists(newPath))
  File.Copy(oldPath, newPath);
 else
  string.Format("The file {0} already exists in the current directory.", Path.GetFileName(newPath)).Show();
}
    

The file is copied without incident. However, when the user tries to select a file they just copied to display, A file not found exception. After debugging, I've found that the UriSource for the dynamic image is resolving the relative path 'Files{selected file}' to the directory that was just browsed by the file select in the above code instead of the Application directory as it seems like it should.

This problem only occurs when a newly copied file is selected. If you restart the application and select the new file it works fine.

Here's the code that dynamically sets the Image source:

    
//Cover = XAML Image
Cover.Source(string.Format(@"Files\{0}\{1}", item.ItemID, item.CoverImage), "carton.ico");

...

public static void Source( this Image image, string filePath, string alternateFilePath)
{
    try
 {image.Source = GetSource(filePath);}
    catch(Exception)
 {image.Source = GetSource(alternateFilePath);}
}

private static BitmapImage GetSource(string filePath)
{
    var source = new BitmapImage();
    source.BeginInit();
    source.UriSource  = new Uri( filePath, UriKind.Relative);
    //Without this option, the image never finishes loading if you change the source dynamically.
    source.CacheOption = BitmapCacheOption.OnLoad;
    source.EndInit();
    return source;
}
    

I'm stumped. Any thought's would be appreciated.


Although I don't have a direct answer, you should use caution for such allowing people to upload files. I was at a seminar where they had good vs bad hackers to simulate real life exploits. One was such that files were allowed to be uploaded. They uploaded malicious asp.net files and called the files directly as they new where the images were ultimately presented to the users, and were able to eventually take over a system. You may want to verify somehow what TYPES of files are being allowed and maybe have stored in a non-exeucting directory of your web server.


It turns out I was missing an option in the constructor of my openfiledialogue. The dialogue was changing the current directory which was causing the relative paths to resolve incorrectly.

If you replace the open file with the following:


var open = new OpenFileDialog{ Multiselect = true, Filter = "AllFiles|*.*", RestoreDirectory = true};

The issue is resolved.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号