我正在使用dropzone上传图片。图像将上传到服务器文件夹,而图像名称将保存在数据库表中。
这是我的代码:
public class 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张图片。那么如何确定图像名称将在哪一列?????
任何建议??????
答案 0 :(得分:0)
我不确定确切的语法,但这样的事情可行。
// **********************************
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);
}