处理来自OpenFileDialog的多个选定文件

时间:2015-07-20 12:33:31

标签: c# sql-server filestream

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

2 个答案:

答案 0 :(得分:5)

在你的循环中,你正在使用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

答案 1 :(得分:-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);
            }