2017-07-24 96 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

相關問題