如何上传多个文件并使用ajaxfileupload将它们插入数据库

时间:2013-07-03 04:28:10

标签: c# asp.net file-upload ajaxcontroltoolkit

我使用AjaxFileUpload控件上传多个图像文件。我正在做的是将文件保存到一个文件夹,然后将其插入数据库。但我收到上传错误。该文件既不会上传到文件夹中,也不会保存在表格中。

这是我的aspx代码:

 <div id="sliderinsert" style="position:absolute; padding-bottom:5px; background-color:gray; top: 24px; left: 13px; 
        width: 695px; height: 300px; margin-bottom: 1px; overflow:visible">

            <ajaxToolkit:AjaxFileUpload style="position:absolute; width:695px; overflow:auto;max-height:600px"  MaximumNumberOfFiles="7" AllowedFileTypes="jpg,jpeg,png,gif"  
                id="ajaxUpload1"  OnUploadComplete="ajaxUpload1_UploadComplete" runat="server" Width="633px"/>

            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>

        </div>

这是我背后的代码:

protected void ajaxUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
         id+=1;
        string filename = Path.GetFileName(e.FileName);
        string filepath = Server.MapPath("~/Images/Gallery/" + filename);
        ajaxUpload1.SaveAs(filepath);

        string Insert = "Insert into slider (slid,slurl) values (@id,@IMAGE_PATH)";
        SqlCommand cmd = new SqlCommand(Insert, con);
        cmd.Parameters.AddWithValue("@IMAGE_PATH", filepath);
        cmd.Parameters.AddWithValue("id", id);
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
            cmd.Dispose();
        }    
    }

在PageLoad事件中我得到表的最大值(id),然后在上传完成事件中递增它,然后将文件插入表中。

任何人都可以告诉我我在这里做的错误是什么。请帮帮我...

2 个答案:

答案 0 :(得分:2)

如果您是第一次将图像保存在应用程序文件夹中,请确保 〜/ Images / Gallery / path存在与否。

您可以直接使用参数将值插入滑块表中。尝试以下代码

protected void ajaxUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    id += 1;
    string filename = Path.GetFileName(e.FileName);
    string filepath = Server.MapPath("~/Images/Gallery/" + filename);
    ajaxUpload1.SaveAs(filepath);

    string Insert = "Insert into slider (slid,slurl) values (" + @id + ",'" + @IMAGE_PATH + "')"; 
    SqlCommand cmd = new SqlCommand(Insert, con);
    cmd.CommandType = CommandType.Text;
    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        con.Close();
        cmd.Dispose();
    }
}

答案 1 :(得分:2)


检查以下给定代码:

protected void AjaxFileUpload1_UploadComplete1(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)<br>
    {
        string filepath = (Server.MapPath("~/Images/") +Guid.NewGuid()+ System.IO.Path.GetFileName(e.FileName));<br>
        AjaxFileUpload1.SaveAs(filepath);<br>
        string fl = filepath.Substring(filepath.LastIndexOf("\\"));<br>
        string[] split = fl.Split('\\');<br>
        string newpath = split[1];<br>
        string imagepath = "~/Images/" + newpath;<br>
        string Insert = "Insert into IMAGE_PATH (IMAGE_PATH) values (@IMAGE_PATH)";<br>
        SqlCommand cmd = new SqlCommand(Insert, con);<br>
        cmd.Parameters.AddWithValue("@IMAGE_PATH", newpath);<br>
        con.Open();<br>
        cmd.ExecuteNonQuery();<br>
        con.Close();<br>
        cmd.Dispose();<br>

    }