2013-07-23 137 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()中完成图像上载时调用它()

相关问题