你好编码器我有一个 fileupload按钮,它用于从系统插入图像并将它们保存到数据库中我现在想要的是选择多个图像但在中仅使用单个文件上传器框架4.0
每张图片都有优先权
代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
Boolean enable = true;
string relativepath = "https://Images/Module/" + ddlPPT.SelectedValue + "/";
//Get imagename from fileupload control
string imgName = fileuploadimages.FileName.ToString();
//sets the image path if exist then store image in that place else create new one
string imgPath = "images/Modules/" + "" + ddlPPT.SelectedValue + "/";
bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
if (!IsExists)
System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
//then save it to the Folder
fileuploadimages.SaveAs(Server.MapPath(imgPath + imgName));
//Open the database connection
con.Open();
//Query to insert * into Images_Master into database
SqlCommand cmd = new SqlCommand("Insert into Image_Master(Userid,Image_Name,Description,ModuleId,pptId,Priority,Imageurl,RelativePath,IsEnable,dt) values('"+Session["trainer"]+"','" + imgName + "','" + tbDescription.Text + "','" + ddlModule.SelectedValue + "','" + ddlPPT.SelectedValue + "','" + lblPriority.Text + "',@Imageurl,'" + relativepath + "','" + enable + "','" + DateTime.Now + "')", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@Image_Name", imgName);
cmd.Parameters.AddWithValue("@Description", tbDescription.Text.Trim());
cmd.Parameters.AddWithValue("@ModuleId", ddlModule.SelectedValue);
cmd.Parameters.AddWithValue("@pptId", ddlPPT.SelectedValue);
cmd.Parameters.AddWithValue("@Priority", lblPriority.Text);
cmd.Parameters.AddWithValue("@Imageurl", imgPath + imgName);
cmd.Parameters.AddWithValue("@RelativePath", relativepath);
cmd.ExecuteNonQuery();
tbDescription.Text = "";
Logs.InsertLogs(Session["Role"].ToString() + ":" + Session["trainer"].ToString(), "CreateModule.aspx.cs btn_Click", "Query Successfully Executed to insert image");
Response.Write("<script>alert('Image uploaded..!!!');</script>");
}
catch (Exception ex2)
{
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
在这个i选择文件中使用文件上传按钮并点击btnSubmit将图像细节保存在数据库中现在我想用上传按钮选择多个图像并点击btnsubmit数据保存在数据库中 在此先感谢!!!!
答案 0 :(得分:0)
试试这个
string[] files = Directory.GetFiles(Server.MapPath(imgPath));
List<string> filenames = new List<string>();
for (int i = 0; i < files.Length; i++)
{
try
{
Boolean enable = true;
string relativepath = "https://Images/Module/" + ddlPPT.SelectedValue + "/";
//Get imagename from fileupload control
string imgName = fileuploadimages.FileName.ToString();
//sets the image path if exist then store image in that place else create new one
string imgPath = "images/Modules/" + "" + ddlPPT.SelectedValue + "/";
bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
if (!IsExists)
System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
//then save it to the Folder
fileuploadimages.SaveAs(Server.MapPath(imgPath + Path.GetFileName(files[i])));
//Open the database connection
con.Open();
//Query to insert * into Images_Master into database
SqlCommand cmd = new SqlCommand("Insert into Image_Master(Userid,Image_Name,Description,ModuleId,pptId,Priority,Imageurl,RelativePath,IsEnable,dt) values('"+Session["trainer"]+"','" + Path.GetFileName(files[i]) + "','" + tbDescription.Text + "','" + ddlModule.SelectedValue + "','" + ddlPPT.SelectedValue + "','" + lblPriority.Text + "',@Imageurl,'" + relativepath + "','" + enable + "','" + DateTime.Now + "')", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@Image_Name", Path.GetFileName(files[i]));
cmd.Parameters.AddWithValue("@Description", tbDescription.Text.Trim());
cmd.Parameters.AddWithValue("@ModuleId", ddlModule.SelectedValue);
cmd.Parameters.AddWithValue("@pptId", ddlPPT.SelectedValue);
cmd.Parameters.AddWithValue("@Priority", lblPriority.Text);
cmd.Parameters.AddWithValue("@Imageurl", imgPath + Path.GetFileName(files[i]));
cmd.Parameters.AddWithValue("@RelativePath", relativepath);
cmd.ExecuteNonQuery();
tbDescription.Text = "";
Logs.InsertLogs(Session["Role"].ToString() + ":" + Session["trainer"].ToString(), "CreateModule.aspx.cs btn_Click", "Query Successfully Executed to insert image");
Response.Write("<script>alert('Image uploaded..!!!');</script>");
}
catch (Exception ex2)
{
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
答案 1 :(得分:0)
默认FileUploader控件的替代方法是使用AjaxFileUploader控件。您可以选择只有一个按钮的多个文件,如您的情况。
只需将其放入标记:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
onuploadcomplete="AjaxFileUpload1_UploadComplete" AllowedFileTypes="jpg,jpeg"
MaximumNumberOfFiles="10"/>
在代码隐藏中添加此方法:
protected void AjaxFileUpload1_UploadComplete(Object sender, AjaxFileUploadEventArgs e)
{
// This method will be called after uploading each file
if (e.FileName != String.Empty)
{
string relativepath = "https://Images/Module/" + ddlPPT.SelectedValue + "/";
//Get imagename from fileupload control
string imgName = e.FileName.ToString();
//sets the image path if exist then store image in that place else create new one
string imgPath = "images/Modules/" + "" + ddlPPT.SelectedValue + "/";
bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
if (!IsExists)
System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
//then save it to the Folder
AjaxFileUpload1.SaveAs(Server.MapPath(imgPath + imgName));
//Open the database connection
con.Open();
//Query to insert * into Images_Master into database
SqlCommand cmd = new SqlCommand("Insert into Image_Master(Userid,Image_Name,Description,ModuleId,pptId,Priority,Imageurl,RelativePath,IsEnable,dt) values('" + Session["trainer"] + "','" + imgName + "','" + tbDescription.Text + "','" + ddlModule.SelectedValue + "','" + ddlPPT.SelectedValue + "','" + lblPriority.Text + "',@Imageurl,'" + relativepath + "','" + enable + "','" + DateTime.Now + "')", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@Image_Name", imgName);
cmd.Parameters.AddWithValue("@Description", tbDescription.Text.Trim());
cmd.Parameters.AddWithValue("@ModuleId", ddlModule.SelectedValue);
cmd.Parameters.AddWithValue("@pptId", ddlPPT.SelectedValue);
cmd.Parameters.AddWithValue("@Priority", lblPriority.Text);
cmd.Parameters.AddWithValue("@Imageurl", imgPath + imgName);
cmd.Parameters.AddWithValue("@RelativePath", relativepath);
cmd.ExecuteNonQuery();
tbDescription.Text = "";
Logs.InsertLogs(Session["Role"].ToString() + ":" + Session["trainer"].ToString(), "CreateModule.aspx.cs btn_Click", "Query Successfully Executed to insert image");
//you don't response.write here since the AjaxFileUploader already notify when image uploaded successfully
//Response.Write("<script>alert('Image uploaded..!!!');</script>");
}
}
这样您就可以将多个文件下载到服务器并将每个文件URL保存在数据库中。