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();
            }




    }
}

谢谢

4

3 回答 3

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);
}
于 2013-07-23T09:00:18.810 回答
0

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();
        }
于 2013-07-23T08:45:06.393 回答
0

该人仅单击一次上传按钮 - 因此仅保存一张图像。

就我个人而言,我会评估您编写此代码的方式。我会将用于将图像保存到数据库的代码移动到独立方法中,并在 GetPictureData() 中完成图像上传时调用它

于 2013-07-23T08:48:30.940 回答