2017-05-29 92 views
0

我正在使用dropzone上传图像。图像将被上传到服务器文件夹,而图像名称将被保存在数据库表中。如何使用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 [email protected] where [email protected] 

    "; 
    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张图片。所以如何决定图像名称将在哪一列出现?

任何建议??????

+0

不要紧哪一列?如果不是的话,你可以按照循环的顺序来做,每次增加1。 – Icewine

+0

列名无所谓! – ace

+0

这里的问题是,如果用户一次上传4张图片。我将如何编写查询n代码来上传这4个图像? string strSql = @“update tablename set image = @ image where id = @ Id – ace

回答

0

我不确定确切的语法,但类似的东西可以工作。

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

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

 
     if (!string.IsNullOrEmpty(fileName)) 
 
     { 
 
     
 
\t \t \t // ********************************** 
 
\t \t \t counter++; // increment the counter 
 
     // ********************************** 
 
\t \t \t 
 
      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. 
 
// ********************************** 
 

 
     } 
 
    } 
 
\t 
 

 
// ********************************** 
 
// 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 [email protected][email protected] where [email protected]"; 
 
    // ********************************** 
 
    
 
    SqlParameter[] objSqlParameter ={ 
 
             new SqlParameter("@strImage",strImage), 
 
             new SqlParameter("@Id",Id) 
 
            }; 
 
    SqlHelper.ExecuteNonQuery(strConnectionString, CommandType.Text, strSql, objSqlParameter); 
 
}

+0

string strSql = @”update tablename set img @ columnName = @ image where id = @ Id“; 该行如何决定列要插入图像的名称????列:img1,img2,img3,img4 ?? – ace

+0

img @ columnName = @ image其中img是列名加上变量columnName – Icewine

+0

String column =“img”+ columnName which would如果你将最大上传设置为4,它将从1 => 4开始,所以函数将运行四次,递增计数器和列名img1, img2,img3,img4 – Icewine

相关问题