2013-07-23 135 views
1

我的多圖像上傳代碼正常工作。但是,在數據庫中插入圖像路徑url時,只會保存一個圖像路徑。我怎樣才能一次保存所有的圖像路徑網址。同時在數據庫中保存多個圖像路徑url

這裏是我的GetPictureData()

public string GetPictureData() 
{ 
    string retFileName = ""; 
    try 
    { 
     if (((FileUpload1.PostedFile != null))) 
     { 
      if ((FileUpload1.PostedFile.ContentType.ToUpper().Contains("IMAGE"))) 
      { 
       HttpFileCollection hfc = Request.Files; 
       for (int i = 0; i < hfc.Count; i++) 
       { 
        HttpPostedFile hpf = hfc[i]; 
        if (hpf.ContentLength > 0) 
        { 
         //Stream inStream = hpf.InputStream; 
         //byte[] fileData = new byte[hpf.ContentLength]; 
         //inStream.Read(fileData, 0, hpf.ContentLength); 

         String sTimeStamp = GetTimeStamp(); 
         string iFileName = System.IO.Path.GetFileName(hpf.FileName); 
         string newFileName = iFileName.Replace(" ", "_"); 
         string OutFile = Server.MapPath(ConfigurationManager.AppSettings["LocalImageDirectory"]) + "\\" + sTimeStamp + "_" + newFileName; 
         hpf.SaveAs(OutFile); 
         OutFile = ConfigurationManager.AppSettings["LocalImageDirectory"] + "\\" + sTimeStamp + "_" + newFileName; 
         retFileName = OutFile; 
        } 
       } 
      } 
     } 
    } 
    catch(Exception ex) 
    { 
     string msg = ex.Message; 
     Response.Write(msg); 
    } 

    return retFileName; 

} 

這裏是我UploadButton代碼

protected void btnUpload_Click(object sender, EventArgs e) 
{ 
    if (Session["localauctionid"] != null && Session["localauctionid"].ToString() != "") 
    { 

     string filepath = GetPictureData(); 

      if (filepath != "") 
      { 
       string sqlcommand = " insert into auctionimages (auctionid, ImagePath, GalleryPic) values(" + Session["localauctionid"].ToString() + ",'" + filepath + "', 0);" + 
            " update auctionstep1 set ListingStatus = 'Photographed' where auctionid = " + Session["localauctionid"].ToString() + " and (listingstatus <> 'Created' AND listingstatus <> 'Saved');"; 
       Database db = DatabaseFactory.CreateDatabase(); 
       DbCommand cmd = db.GetSqlStringCommand(sqlcommand); 
       db.ExecuteNonQuery(cmd); 
       LoadImages(); 
      } 




    } 
} 

感謝

回答

1

您錯誤在於GetPictureData循環文件,但只有最後一個文件返回到您調用保存到數據庫代碼的按鈕事件。當然,只有最後一個文件將被保存在數據庫中。

解決方法是創建一個獨立的方法來保存您傳遞文件名和localAuctionID的數據庫。您調用GetPictureData這裏面的方法(更正確更名爲SavePictureData)內部循環的每個文件保存

作爲一個僞代碼(未測試)

private void SaveToDb(int localAutID, string filepath) 
{ 
    string sqlcommand = " insert into auctionimages (auctionid, ImagePath, GalleryPic) " + 
     "values(@auID, @file, 0); " + 
     " update auctionstep1 set ListingStatus = 'Photographed' " + 
     "where auctionid = @auID and (listingstatus <> 'Created' " + 
     "AND listingstatus <> 'Saved');"; 
     Database db = DatabaseFactory.CreateDatabase(); 
     DbCommand cmd = db.GetSqlStringCommand(sqlcommand); 
     DbParameter p1 = cmd.CreateParameter() 
         {ParameterName="@auID", DbType=DbType.Int32, Value=localAutID}; 
     DbParameter p2 = cmd.CreateParameter() 
         {ParameterName="@file", DbType=DbType.AnsiString, Value=filepath}; 
     db.ExecuteNonQuery(cmd); 
} 

如果SavePictureData調用它裏面的for循環

for (int i = 0; i < hfc.Count; i++) 
{ 
    ..... 
    retFileName = OutFile; 
    SaveToDb(Convert.ToInt32(Session["localauctionid"]), retFileName); 
} 
+0

謝謝你指導我@Steve。成功完成。 –

0

如果(會話[ 「localauctionid」]!= NULL & &會話[ 「localauctionid」]。ToString()!=「」) {

string filepath = GetPictureData(); 

     if (filepath != "") 
     { 
      string sqlcommand = " insert into auctionimages (auctionid, ImagePath, GalleryPic) values(" + Session["localauctionid"].ToString() + ",'" + filepath + "', 0);" + 
           " update auctionstep1 set ListingStatus = 'Photographed' where auctionid = " + Session["localauctionid"].ToString() + " and (listingstatus <> 'Created' AND listingstatus <> 'Saved');"; 
      Database db = DatabaseFactory.CreateDatabase(); 
      DbCommand cmd = db.GetSqlStringCommand(sqlcommand); 
      db.ExecuteNonQuery(cmd); 
      LoadImages(); 
     } 
0

該人只點擊一次上傳按鈕 - 因此只保存一個圖像。

就我個人而言,我會評估你編碼的方式。我將把用於將圖像保存到數據庫的代碼轉換爲獨立的方法,並在GetPictureData()中完成圖像上載時調用它()

相關問題