【问题标题】:Process multiple selected files from OpenFileDialog从 OpenFileDialog 处理多个选定的文件
【发布时间】:2015-07-20 12:33:30
【问题描述】:

我使用 foreach 循环读取多个图像文件,但我只能流式传输第一个选定的文件。当我尝试保存多个不同的图像时,输出就像是第一个选定图像的精确副本,而不是剩余的不同图像。

    private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(DBHandler.GetConnectionString());
        try
        {
            OpenFileDialog fop = new OpenFileDialog();
            fop.Multiselect = true;

            fop.InitialDirectory = "C:\\";
            fop.Filter = "JPG,JPEG|*.jpg|PNG|*png";
            if (fop.ShowDialog() == DialogResult.OK)
            {

                foreach (String files in fop.FileNames)
                {
                    FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read);
                    byte[] img = new byte[FS.Length];
                    FS.Read(img, 0, Convert.ToInt32(FS.Length));

                    if (con.State == ConnectionState.Closed)
                        con.Open();
                    SqlCommand cmd = new SqlCommand("SaveImage", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@img", SqlDbType.Image).Value = img;
                    cmd.ExecuteNonQuery();

                }

                MessageBox.Show("Image has been saved successfully!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

我想在同一个表单上查看所有图像。

我的期望:A-B-C-D (每个字母代表不同的检索图像。“A”是第一个从对话框中选择的文件)

实际输出:A-A-A-A。为什么会这样?

【问题讨论】:

  • 对您的代码的一些建议。您的 SqlConnection 应该在 using() 中。我建议对 FileStream 使用相同的方法,或者至少在完成后使用所有 FS.Close()。您不想留下不必要的资源分配。

标签: c# sql-server filestream


【解决方案1】:

在您的循环中,您使用的是fop.FileName,它返回第一个选定的文件:

此属性只能是一个选定文件的名称。如果要在多选对话框中返回包含所有选定文件名称的数组,请使用 FileNames。

foreach (String files in fop.FileNames)
{
    FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read);

    // ...
}

改为使用迭代变量filename

foreach (String filename in fop.FileNames)
{
    FileStream FS = new FileStream(filename, FileMode.Open, FileAccess.Read);

    // ...
}

相关:OpenFileDialog reads only the first file.

【讨论】:

  • 哎呀我刚刚意识到我打错了您的解决方案。没有错误很抱歉给您带来不便:) + 感谢您的帮助。试图使用对话框本身放置在循环中。看起来很傻:)
【解决方案2】:

你的代码应该是这样的

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(DBHandler.GetConnectionString());
        try
        {
            OpenFileDialog fop = new OpenFileDialog();
            fop.Multiselect = true;

            fop.InitialDirectory = @"C:\";
            fop.Filter = "JPG,JPEG|*.jpg|PNG|*png";
            if (fop.ShowDialog() == DialogResult.OK)
            {

                foreach (String files in fop.FileNames)
                {
                    FileStream FS = new FileStream(@files, FileMode.Open, FileAccess.Read);
                    byte[] img = new byte[FS.Length];
                    FS.Read(img, 0, Convert.ToInt32(FS.Length));

                    if (con.State == ConnectionState.Closed)
                        con.Open();
                    SqlCommand cmd = new SqlCommand("SaveImage", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@img", SqlDbType.Image).Value = img;
                    cmd.ExecuteNonQuery();

                }

                MessageBox.Show("Image has been saved successfully!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

【讨论】:

  • 转储“固定”代码并不能帮助任何人学习任何东西。解释你看到了什么问题,是什么原因造成的,以及你是如何解决的。还可以尝试在发布前点击“新答案”栏,看看您是否发布了重复的内容。
  • 我一定会在未来解决这个问题。谢谢@CodeCaster。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
相关资源
最近更新 更多