简体   繁体   中英

Save Files In WPF Application. C#

I am wondering how to store files uploaded on the page into the settings?

For example text files

That's because you don't save the image. I'm a bit confused about what you mean by upload though. But I try to explain what I think is gonna help you.

You should do your "Upload" somewhere around Save the image :

if (op.ShowDialog() == true)
{
    //Save the image
    image1.Source = new BitmapImage(new Uri(op.FileName));
    MessageBox.Show("File uploaded sucessfully");
}

The closest thing that comes to mind by upload is that you want to upload the image to a server. For that you're gonna have to use a web service that receives the image data. In the next page refresh though you will need another service to receive the previously uploaded image and show it to user.

If you want your application to store user images in it's file system (like an album application for example) then there are two modes.

1- Keeping the files in their places and storing their URL in this case in Save the image part you store the op.FileName and in the next load try to load the image from this address. You should handle file not available of course.

2- Store a copy of the image for safe usage when user removes the original file. If this is the case you should store the whole image in Save the image part and load it next time from your safe location inside user file system.

Hope I get your problem and this helps you :)

-- Edit --

All right after the clarification in the comments I can elaborate more:

You need a functions to create Base64 using your images:

public string FileToBase64(string Path)
{
    byte[] imageArray = System.IO.File.ReadAllBytes(Path);
    return Convert.ToBase64String(imageArray);
}

And another one to store an string in DB. This part is dependent on your DB structure and technology but I'm sure you can figure it out:

public void SaveTheImageToDb(string Data) { }

And a converter to assign your strings to WPF images:

public class Base64ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = value as string;

        if (s == null)
            return null;

        BitmapImage bi = new BitmapImage();

        bi.BeginInit();
        bi.StreamSource = new MemoryStream(System.Convert.FromBase64String(s));
        bi.EndInit();

        return bi;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then in your code:

if (op.ShowDialog() == true)
{
    var imageData = FileToBase64(op.FileName);
    SaveTheImageToDb(imageData);
    image1.Source = new BitmapImage(new Uri(op.FileName));
    MessageBox.Show("File uploaded sucessfully");
}

And in the next loading of the view you can use binding and the converter to show the stored image from Db.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM