0

我在 SQL Server 数据库中有一些图像,我想在 ASP.NET 中检索这些图像。但这给了我一个错误

文件“C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\130065367816821657”已经存在。

请解决我的问题。

protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
        cn.Open();
        SqlCommand cm = new SqlCommand("select * from imageCollection where img_id='" + DropDownList1.SelectedItem.ToString() + "'", cn);
        SqlDataAdapter da = new SqlDataAdapter(cm);
        SqlDataReader dr = cm.ExecuteReader();

        try
        {
            if (dr.Read())
            {

                string image1 = Convert.ToString(DateTime.Now.ToFileTime());
                string image2 = Convert.ToString(DateTime.Now.ToFileTime());
                FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
                FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);
                byte[] bimage1 = (byte[])dr["passport_photo"];
                byte[] bimage2 = (byte[])dr["sign_photo"];
                fs1.Write(bimage1, 0, bimage1.Length - 1);
                fs2.Write(bimage2, 0, bimage2.Length - 1);
                fs1.Flush();
                fs2.Flush();
                Image1.ImageUrl = "~/images"+bimage1.ToString();
                Image2.ImageUrl = "~/images"+bimage2.ToString();
            }
            dr.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }

我从“C:\Program Files\Common Files\microsoft shared\DevServer\10.0”上传了图片,前端代码如下:

 protected void Button1_Click(object sender, EventArgs e)
{
    string image1 = FileUpload1.FileName;
    string image2 = FileUpload2.FileName;
    FileStream fs1 = new FileStream(image1, FileMode.Open, FileAccess.Read);
    FileStream fs2 = new FileStream(image2, FileMode.Open, FileAccess.Read);
    byte[] bimage1 = new byte[fs1.Length];
    byte[] bimage2 = new byte[fs2.Length];
    fs1.Read(bimage1, 0, Convert.ToInt32(fs1.Length));
    fs2.Read(bimage2, 0, Convert.ToInt32(fs2.Length));
    fs1.Close();
    fs2.Close();
    cn.Open();
    SqlParameter sp = new SqlParameter();
    sp.SqlDbType = SqlDbType.Image;
    sp.ParameterName = "@passport_photo";
    sp.ParameterName = "@sign_photo";
    sp.Value = bimage1;
    sp.Value = bimage2;
    SqlCommand cm = new SqlCommand("INSERT INTO imageCollection values(@img_id," + "@passport_photo,"+"@sign_photo)", cn);
    cm.Parameters.AddWithValue("@img_id",TextBox1.Text);
    cm.Parameters.AddWithValue("@passport_photo",sp.Value=bimage1);
    cm.Parameters.AddWithValue("@sign_photo",sp.Value=bimage2);
    cm.ExecuteNonQuery();
    cm.Dispose();
    cn.Dispose();
    cn.Close();
}

}

4

2 回答 2

5

你的问题是,image1并且image2是平等的。

string image1 = Convert.ToString(DateTime.Now.ToFileTime());
string image2 = Convert.ToString(DateTime.Now.ToFileTime());

考虑使用一些东西来区分两个文件名:

string image1 = Convert.ToString(DateTime.Now.ToFileTime()) + "1";
string image2 = Convert.ToString(DateTime.Now.ToFileTime()) + "2";

可以想象,创建文件名所涉及的代码Convert.ToString(DateTime.Now.ToFileTime())执行得非常快,所以没有足够的时间来DateTime.Now增加它的价值。

编辑

您还可以更改写入图像的目录:

 FileStream fs1 = new FileStream(Server.MapPath("~/images/" + image1), FileMode.CreateNew, FileAccess.Write);
 FileStream fs2 = new FileStream(Server.MapPath("~/images/" + image2), FileMode.CreateNew, FileAccess.Write);

否则,文件将写入应用程序文件夹 ( bin)

接着

Image1.ImageUrl = "~/images/" + image1;
Image2.ImageUrl = "~/images/" + image2;

现在有感觉了

确保 ASP.NET 用户在“images”文件夹中具有写入权限。

于 2013-02-28T15:25:16.507 回答
0

此外,如果找到具有重复文件名的文件,您的代码将引发错误:

FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);

以下代码将允许通过更改为来覆盖FileMode.CreateNew文件FileMode.Create

FileStream fs1 = new FileStream(image1, FileMode.Create, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.Create, FileAccess.Write);
于 2013-02-28T15:27:41.303 回答