【问题标题】:How to save multiple image names into database by using dropzone?如何使用 dropzone 将多个图像名称保存到数据库中?
【发布时间】:2017-10-29 07:40:26
【问题描述】:

我正在使用 dropzone 上传图片。图片将上传到服务器文件夹,图片名称将保存在数据库表中。

这是我的代码:

公共类 FormUploader_dz : IHttpHandler {

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";

    string dirFullPath = HttpContext.Current.Server.MapPath("/images/");
    string[] files;
    int numFiles;
    files = System.IO.Directory.GetFiles(dirFullPath);
    numFiles = files.Length;
    numFiles = numFiles + 1;

    string str_image = "";

    foreach (string s in context.Request.Files)
    {
        HttpPostedFile file = context.Request.Files[s];
        //  int fileSizeInBytes = file.ContentLength;
        string fileName = file.FileName;
        string fileExtension = file.ContentType;

        if (!string.IsNullOrEmpty(fileName))
        {
            fileExtension = System.IO.Path.GetExtension(fileName);


            str_image = "MyPHOTO_" + numFiles.ToString() + fileExtension;
            string pathToSave_100 = HttpContext.Current.Server.MapPath("/images/") + str_image;
            file.SaveAs(pathToSave_100);

            Service.SaveImage(strFileName, context.Session["Id"].ToString());

        }
    }
    context.Response.Write(str_image);
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

}

服务文件代码:将图像名称插入表中。

public static void SaveImage(string strImage, string Id)
{
    string strSql = @"update tablename set image=@image where id=@Id

    ";
    SqlParameter[] objSqlParameter ={
                                       new SqlParameter("@image",strImage),
                                       new SqlParameter("@Id",Id)
                                   };
    SqlHelper.ExecuteNonQuery(strConnectionString, CommandType.Text, strSql, objSqlParameter);
}

现在这里的问题是我在表中有 4 列来保存 4 个不同的图像名称。我实际上在做的是允许用户上传最多 4 张图片。我有 4 列,分别是 img1、img2、img3、img4。

这里如何将图像名称插入/更新到表中,因为它们是 4 个不同的图像列! 在这里,如果用户想要他可以上传 4 张图片。那么如何确定图像名称将放在哪一列????

有什么建议吗??????

【问题讨论】:

  • 哪一栏重要吗?如果不是,您可以像循环一样按顺序执行,每次递增 1。
  • 列名无关紧要!
  • 这里的问题是如果用户一次上传 4 张图片。我将如何编写查询代码来上传这 4 张图片? string strSql = @"更新表名 set image=@image where id=@Id

标签: c# asp.net sql-server image dropzone.js


【解决方案1】:

我不确定确切的语法,但类似这样的方法可以工作。

// **********************************
int counter = 0; // set up the counter
// **********************************

 foreach (string s in context.Request.Files)
    {
		
		
        HttpPostedFile file = context.Request.Files[s];
        //  int fileSizeInBytes = file.ContentLength;
        string fileName = file.FileName;
        string fileExtension = file.ContentType;

        if (!string.IsNullOrEmpty(fileName))
        {
        
			// **********************************
			counter++; // increment the counter
      // **********************************
			
            fileExtension = System.IO.Path.GetExtension(fileName);


            str_image = "MyPHOTO_" + numFiles.ToString() + fileExtension;
            string pathToSave_100 = HttpContext.Current.Server.MapPath("/images/") + str_image;
            file.SaveAs(pathToSave_100);
            
// **********************************
            Service.SaveImage(strFileName, context.Session["Id"].ToString(), counter); // add counter variable to the path.
// **********************************

        }
    }
	

// **********************************
// add a column variable and then add it to your column name. like string columnName = "img+columnCount"
// **********************************
public static void SaveImage(string strImage, string Id, int columnCount)
{
    // **********************************
    string strSql = @"update tablename set img@columnName=@image where id=@Id";
    // **********************************
    
    SqlParameter[] objSqlParameter ={
                                       new SqlParameter("@strImage",strImage),
                                       new SqlParameter("@Id",Id)
                                   };
    SqlHelper.ExecuteNonQuery(strConnectionString, CommandType.Text, strSql, objSqlParameter);
}

【讨论】:

  • string strSql = @"update tablename set img@columnName=@image where id=@Id";这条线将如何决定要插入图像的列名????列:img1、img2、img3、img4??
  • img@columnName=@image 其中 img 是列名加上变量 columnName
  • String column = "img"+columnName 这将是您上面循环中传递给函数的整数。如果您将最大上传数设置为 4,它将按 1 => 4 的顺序排列。因此该函数将运行四次,递增计数器并因此增加列名。 img1, img2, img3, img4
猜你喜欢
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 2018-03-15
相关资源
最近更新 更多