【问题标题】:how to upload file using wpf and c# in windows application如何在windows应用程序中使用wpf和c#上传文件
【发布时间】:2014-07-23 09:29:13
【问题描述】:

我有一个带有浏览按钮的表单,当我点击它时,对话框打开并且我选择了一个文件,现在该文件的路径或文件名将显示在文本框中我已经编写了代码但文件路径不是显示在文本框中,我尝试解决但失败了,请让我摆脱这个,还有一件事,我想把它上传到特定文件夹然后下载到我的系统上,请告诉我这个也

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.IO;
//using System.Drawing;
using System.ComponentModel;



namespace RIS_2
{
    /// <summary>
    /// Interaction logic for crud.xaml
    /// </summary>
    public partial class crud : Window
    {
        public crud()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
        }

        private void browse_btn_Click(object sender, RoutedEventArgs e)
        {
            Stream checkStream = null;
         //   OpenFileDialog op1 = new OpenFileDialog();
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Multiselect = false;
            dlg.Filter = "All Image Files | *.*";
            //dlg.Filter = "(*.pfw)|*.pfw|All files (*.*)|*.*";
           Nullable<bool> result = dlg.ShowDialog();
            // if ((bool)dlg.ShowDialog())
            if(result==true)
            {
                try
                {
                    if ((checkStream = dlg.OpenFile()) != null)
                    { 
                    //   MyImage.Source = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
                        //listBox1.Items.Add(openFileDialog.FileName);
                        string filename = dlg.FileName;
                        tb_file.Text = filename;
                       // tb_file.AppendText(dlg.FileName.ToString());
                        MessageBox.Show("Successfully done", filename);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }

            }
             else
             {

                 MessageBox.Show("Problem occured, try again later");

             }

        }


    }
}

【问题讨论】:

  • 我想将它上传到特定文件夹,然后下载到我的系统上...这是对您要求的糟糕描述。显示选中的文件路径后,你到底要做什么?
  • 首先我要上传它,上传成功后,另一个用户可以下载它或通过引用该文件的另一个表单查看它
  • 但是上传到哪里下载???月亮?我们不知道...如果您真的确实需要帮助,请与我们分享您的要求。
  • 实际上我想将文件存储在数据库中(pdf,图像,doc等),然后其他用户可以从数据库中下载它

标签: c# .net wpf windows


【解决方案1】:

坦率地说,对我来说,代码看起来还不错,所以奇怪的是它不起作用。话虽如此,我建议您采取不同的方法。您正在使用 WPF,它非常擅长数据绑定。如果你在代码后面定义一个属性

private string _fileName;
public string FileName
{
 get{return _fileName;}
 set{
  _fileName = value;
  OnPropertyChanged("FileName");
}

OnPropertyChanged的实现和INotifyPropertyChanged的简单解释可以在http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/RaisethePropertyChangedevent.htm找到

在您的 XAML 中,您已经有一个 TextBlock,应该使用绑定进行扩展

TextBlock x:Name="tb_file" Text="{Binding Path=Filename}"

为确保绑定到后面代码中的属性有效,请参考Binding objects defined in code-behind

希望对您有所帮助(至少是您问题的第一部分)

【讨论】:

  • @user3667841,如果这个回答帮助你解决了你的问题,那么请accept it,按照本网站的惯例。
猜你喜欢
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2015-05-31
  • 2011-08-12
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多