2

我正在使用 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 事件中,我得到了表的 max(id),并在 Upload complete 事件中增加了它,然后将文件插入到表中。

谁能告诉我我在这里做的错误是什么。请帮我解决这个问题...

4

2 回答 2

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>

    }
于 2013-07-27T06:34:32.203 回答
2

如果您首先将图像保存在应用程序文件夹中,请确保 ~/Images/Gallery/ 路径存在与否。

您可以直接将值插入到滑块表中,而无需使用参数。试试下面的代码

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();
    }
}
于 2013-07-03T04:52:53.973 回答