2017-05-29 7 views
0

私は画像をアップロードするためにdropzoneを使用しています。イメージはサーバフォルダにアップロードされ、イメージ名はデータベーステーブルに保存されます。dropzoneを使用して複数のイメージ名をデータベースに保存する方法は?

パブリッククラスFormUploader_dz:

は、ここに私のコードですIHTTPハンドラ {

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]image 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枚の画像をアップロードする場合です。これらの4つの画像をアップロードするためにクエリnコードをどのように書くのですか? ストリングSTRSQL = @ "更新テーブル名のセット画像= @画像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

ストリングSTRSQL = @" アップデートは、ID ID = @」に設定IMG @ COLUMNNAME = @画像をTABLENAME; どのようにこの行が列を決定しますimg1、img2、img3、img4? – ace

+0

img @ columnName = @ image imgは列名に変数columnNameを加えたものです。 – Icewine

+0

文字列= "img" + columnName関数に渡されたループの整数です。最大アップロード数を4に設定すると、1 => 4から順番に実行されますので、関数が4回実行され、カウンタが増えて列名が増えます。img1、 img2、img3、img4 – Icewine

関連する問題