2017-07-24 65 views
0

我有一个名为与MultipleImageID,MultipleImageName,MultipleImageMap和PropertyID(外键)图像的MySQL表。我遇到的问题是,当我上传它们时,实际上图像似乎重复,但列中填充了正确的信息。这是一张更好解释的图片。多图像路径上传到MySQL数据库

[![http://i.imgur.com/oT5GETG.png]]

正如你所看到的图像名称都是不同的,因为每个图像应该是不同的,但选择的第一个形象似乎多次上传。其他时候,正确的图像将上传,这使我困惑的是什么导致了这一点。我的代码也没有出现错误。

这是我的c#上传。

protected void Insert(object sender, EventArgs e) 
{ 
    string PropertyName = txtName.Text; 
    string PropertyFeatures = txtPropFeat.Text; 
    string PropertyLocation = txtPropLoc.Text; 
    string PropertyInformation = txtPropInfo.Text; 
    string PropertyNumBeds = txtNumBeds.Text; 
    string PropertyPrice = txtPrice.Text; 
    string PropertyType = txtPropType.Text; 
    long InsertedID; 

    string constr = ConfigurationManager.ConnectionStrings["realestatedbAddConString"].ConnectionString; 

    using (MySqlConnection con = new MySqlConnection(constr)) 
    { 
      using (MySqlCommand cmd = new MySqlCommand("INSERT INTO property (PropertyName, PropertyNumBeds, PropertyType, PropertyPrice, PropertyFeatures, PropertyLocation, PropertyInformation, ImageName, ImageMap) VALUES (@PropertyName, @PropertyNumBeds, @PropertyType, @PropertyPrice, @PropertyFeatures, @PropertyLocation, @PropertyInformation, @ImageName, @ImageMap)")) 
      { 
       using (MySqlDataAdapter sda = new MySqlDataAdapter()) 
       { 
        cmd.Parameters.AddWithValue("@PropertyName", PropertyName); 
        cmd.Parameters.AddWithValue("@PropertyNumBeds", PropertyNumBeds); 
        cmd.Parameters.AddWithValue("@PropertyPrice", PropertyPrice); 
        cmd.Parameters.AddWithValue("@PropertyType", PropertyType); 
        cmd.Parameters.AddWithValue("@PropertyFeatures", PropertyFeatures); 
        cmd.Parameters.AddWithValue("@PropertyLocation", PropertyLocation); 
        cmd.Parameters.AddWithValue("@PropertyInformation", PropertyInformation); 

        string FileName = Path.GetFileName(MainImageUploada.FileName); 
        MainImageUploada.SaveAs(Server.MapPath("ImagesUploaded/") + FileName); 

        cmd.Parameters.AddWithValue("@ImageName", FileName); 
        cmd.Parameters.AddWithValue("@ImageMap", "ImagesUploaded/" + FileName); 

        cmd.Connection = con; 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
        InsertedID = cmd.LastInsertedId; 
        con.Close(); 
       } 
      } 
     } 

     if (ImageUploada.HasFiles) 
     { 
      foreach (var file in ImageUploada.PostedFiles) 
      { 
       string FileName1 = Path.GetFileName(ImageUploada.FileName); 
       ImageUploada.SaveAs(Server.MapPath("ImagesUploaded/") + file.FileName); 

       using (MySqlConnection con = new MySqlConnection(constr)) 
       { 
        using (MySqlCommand cmd = new MySqlCommand("INSERT INTO propertyimage(MultipleImageName, MultipleImageMap, PropertyID) VALUES (@MultipleImageName, @MultipleImageMap, @InsertedID); ")) 
        { 
         using (MySqlDataAdapter sda = new MySqlDataAdapter()) 
         { 
          cmd.Parameters.AddWithValue("@MultipleImageName", file.FileName); 
          cmd.Parameters.AddWithValue("@MultipleImageMap", "ImagesUploaded/" + file.FileName); 
          cmd.Parameters.AddWithValue("@InsertedID", InsertedID); 

          cmd.Connection = con; 
          con.Open(); 
          cmd.ExecuteNonQuery(); 
          con.Close(); 
         } 
        } 
       } 

      } 
     } 

     txtName.Text = ""; 
     txtPropFeat.Text = ""; 
     txtPropInfo.Text = ""; 
     txtPropLoc.Text = ""; 
     txtNumBeds.Text = ""; 
     txtPrice.Text = ""; 
     txtPropType.Text = ""; 

     Label1.Visible = true; 
     Label1.Text = "Property Added to Database Successfully!"; 

    } 

我失去了我的代码,数据库还是我使用的图像。

回答

1

你正确地循环着ImageUploada.PostedFiles,但是你忽略了它并且调用了ImageUploada本身的方法,它只处理第一个文件。

因此,如果您上传多个文件,则保存在服务器上的所有文件都将是第一个文件的副本。

您需要更改代码来处理每个file独立:

foreach (var file in ImageUploada.PostedFiles) 
{ 
    string FileName1 = Path.GetFileName(file.FileName); 
    file.SaveAs(Server.MapPath("ImagesUploaded/") + file.FileName); 

    // ... 
} 
+0

完美谢谢! – Aaron177

相关问题