2017-05-29 101 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

相關問題