我希望将所有图像名称存储在数据库中,但只存储一个 [英] i want store all images name in database but storing only one
本文介绍了我希望将所有图像名称存储在数据库中,但只存储一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void btnSave_Click1(object sender, EventArgs e)
{
List<string> img=new List<string>();
HttpFileCollection httpFileCollection = Request.Files;
for (int i = 0; i < httpFileCollection.Count; i++)
{
HttpPostedFile httpPostedFile = httpFileCollection[i];
string fileNameExtension = Path.GetFileName(httpPostedFile.FileName);
if (httpPostedFile.ContentLength > 0)
{
httpPostedFile.SaveAs(Server.MapPath("~/Upload/Testimonial/")+ "\\"+ fileNameExtension);
img.Add(Server.MapPath("~/Upload/Testimonial/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
}
}
if (btnSubmit.Text == "Submit")
{
bool a = video.SaveUpdatetestimonial(1, Editor.Content, img.ToArray().Length > 0 ? string.Join(",", img.ToArray()) : Path.GetFileName(FileUpload1.FileName), txtEmbed.Text, TextBox1.Text, 0);
if (a == true)
{
txtEmbed.Text = "";
TextBox1.Text = "";
Editor.Content = "";
lblInfo.Text = "Record Inserted Successfully";
BindGrid(1);
}
else
{
lblInfo.Text = "Record not Saved";
}
}
else
{
if (btnSubmit.Text == "Update")
{
int Sno = Convert.ToInt32(grdImg.Rows[temp].Cells[1].Text);
bool a = video.SaveUpdatetestimonial(2, Editor.Content, img.ToArray().Length > 0 ? string.Join(",", img.ToArray()) : System.IO.Path.GetFileName(FileUpload1.FileName), txtEmbed.Text, TextBox1.Text,Sno);
if (a == true)
{
txtEmbed.Text = "";
Editor.Content = "";
lblInfo.Text = "Record Updated";
BindGrid(1);
TextBox1.Text = "";
}
else
{
lblInfo.Text = "Record Not Updated";
}
}
}
}
错误是什么?所以它只存储最后选择的图像。帮助我,提前谢谢
what is error in it ? so it is storing only last selecting image. Help me, Thanks in advance
推荐答案
对于此代码,将文件名创建为变量,然后将其添加到集合中。在2行中,您使用了来自2个不同项目的2个不同名称
For this code, create file name as variable and then add it to the collection. In 2 lines you have used 2 different names coming out of 2 different items
httpPostedFile.SaveAs(Server.MapPath("~/Upload/Testimonial/")+ "\\"+ fileNameExtension);
img.Add(Server.MapPath("~/Upload/Testimonial/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
第一行来自httpPostedfFile,另一行来自FileUpload控件。所以这是一个矛盾。文件上传控件将始终具有单个文件名,因此它将是重复输入。
使用saparate变量而不是内联代码将清除代码并提供帮助调试断点
First line is taking from httpPostedfFile and another is taking from FileUpload control. So this is a contradiction. The file upload control will always have a single file name so it will be a repeated entry.
Having saparate variables instead of inline code will clear out the code and help with debugging breakpoints as well
这篇关于我希望将所有图像名称存储在数据库中,但只存储一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文