从打开文件对话框中选择多个文件并处理

3

我使用了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。为什么会这样?

3
关于你的代码,我有一些建议。你的SqlConnection应该在using()语句中使用。我建议FileStream也应该这样做,或者至少在使用完毕后要关闭所有的FS.Close()。你不想让资源不必要地被占用。 - dmeglio
2个回答

6

在您的循环中,您正在使用 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只读取第一个文件


1
抱歉,我刚意识到我打错了你的解决方案。没有错误,对此给您带来的不便表示抱歉 :) 谢谢您的帮助。我试图使用对话框本身来放置在循环中,这似乎非常愚蠢 :) - SophisticatedUndoing

-1

你的代码应该像这样

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
1
我会在以后注意的,谢谢@CodeCaster。 - shreesha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接