开发者

File uploader in WPF

I'm trying to create a custom file upload control in WPF 4.0 and I'm stuck in one point.

I'm not able to save file in my solution folder after browsing the file. Below code I'm using for Browsing

private void btnBrowse_Click(object sender, RoutedEventArgs e)
     开发者_如何转开发   {
            // Create OpenFileDialog

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = "Images (*.JPG;*.JPEG;*.PNG)|*.JPG;*.JPEG;*.PNG";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                FileNameTextBox.Text = filename;
            }
        }

 private void btnUpload_Click(object sender, RoutedEventArgs e)
        {

            string filename = FileNameTextBox.Text;
           // Now I want save this file to my image folder.
        }

Now I want to save file in image folder which is inside my Solution explore. For ASP.NET we use Server.Mappath for mapping the specified relative or virtual path to the corresponding physical directory on the server. But I'm not sure what we can use in WPF for achieving the same thing. I'm new in WPF so please help me.


If you are talking about WPF, and not Silverlight, then it is important to understand the distinction between WPF and ASP.NET. ASP.NET is a hosting platform for the HTTP protocol. Paths in an ASP.NET site are not necessarily directly represented on disk, so Server.MapPath provides a way to map an ASP.NET path to a phsyical path.

WPF, on the other hand, is plain and simply a UI framework. It is not a hosting environment like ASP.NET, so the concept of mapping paths is irrelevant in the context of WPF. Based on the code you provided, you are not "uploading" a file, you are simply opening a file. In WPF, your applications logic is running directly on a users system, and therefor you have access to the file system via the System.IO namespace. You can create a new file using the FileStream class and copy bytes from the source to your new file manually, or simply copy the "opened" file using the File class. Your WPF UI is providing a window into a normal "desktop" application, so uploading files need not (and most likely will not) occur when you open or save a file.

Keep in mind, as your application is a desktop application, you will be limited by the permission set of the user running your application. If the user running your application does not have permission to write somewhere on disk, then you will encounter exceptions when trying to write. The same goes if they do not have permission to read or delete files from somewhere. Make sure any file activities you perform are done in areas of the file system that the user has permission, such as their documents folder.

public void btnUpload_Click(object sender, RoutedEventArgs e)
{
    string filename = FileNameTextBox.Text;
    if (File.Exists(filename))
    {
        // TODO: Show an error message box to user indicating destination file already uploaded
        return;
    }

    string name = Path.GetFileName(filename);
    string destinationFilename = Path.Combine("C:\\temp\\uploaded files\\", name); 

    File.Copy(filename, destinationFilename);

    // TODO: Show information or message box indicating file has copied
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜