简体   繁体   中英

How to save any type of files in a specific folder using file upload in WPF?

I have a TextBox (txtDocUpload) and a Button. On the clicking of that button, an upload dialogue is opening and after uploading a file I have to save it in a particular folder.

For opening the upload dialogue

 private void txtBtnUpload_Click_1(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        //openFileDialog.DefaultExt = ".txt";

        Nullable<bool> result = openFileDialog.ShowDialog();
        if (result == true)
        {
            filename = openFileDialog.FileName;
            txtDocUpload.Text = System.IO.Path.GetFileName(filename);

        }
    }

Clicking save button I have to save, and the code ("File1"is the location where i want to save the file).

string urlpath = "WoDocs";
var path = @"~\" + urlpath + @"\" + WOMaintenance.GetAddressId.IDWorkOrderDetail;
if (!Directory.Exists(path))
    Directory.CreateDirectory(path);

    var ext = System.IO.Path.GetExtension(txtDocUpload.Text);
    var pathURL=txtDocDescription.Text+ext;
    var file1 = System.IO.Path.Combine(path,txtDocDescription.Text + ext);
    //docFile1.SaveAs(file1);

Here is a brief example:

    private void CopyAFile()
    {
        var source = new OpenFileDialog();
        if (source.ShowDialog().GetValueOrDefault())
        {
            var dest = new SaveFileDialog();
            if (dest.ShowDialog().GetValueOrDefault())
            {
                File.Copy(source.FileName, dest.FileName);
            }
        }
    }

This should demonstrate that File.Copy does work when you have access to the source and destination locations.

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