简体   繁体   English

asp.net:AjaxFileUpload OnUploadComplete事件

[英]asp.net : AjaxFileUpload OnUploadComplete event

I'm working to develop simple Drag& Drop Multiple files uploader using Visual Studio 2013 and AjaxControlToolkit v15.0. 我正在使用Visual Studio 2013和AjaxControlToolkit v15.0开发简单的拖放多个文件上传器。 I've got the AjaxFileUpload control but I notice that only one file appears as uploaded on the browser 我有AjaxFileUpload控件,但是我注意到在浏览器中只有一个文件显示为已上传

Also, I see that OnUploadComplete event doesn't fire. 另外,我看到OnUploadComplete事件不会触发。 I test the event by inserting an alert in the event and it does not run also the uploaded file is not shown in the project or database table. 我通过在事件中插入警报来测试事件,该事件不会运行,并且上传的文件也不会显示在项目或数据库表中。

This is my code : 这是我的代码:

.aspx file .aspx文件

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div  align="center">
        <asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server"  MaximumNumberOfFiles="10" OnUploadComplete="File_Upload" Width="500px" Mode="Auto" />
    </div>
</form>

.aspx.cs file .aspx.cs文件

protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
    SqlConnection con = new SqlConnection(abc);
    try {
        string filename = Path.GetFileName(e.FileName);
        string strDestPath = Server.MapPath("~/Documents/");

        SqlCommand cmd = new SqlCommand("INSERT INTO upload (name, path) VALUES(@FileName, @FilePath)");

        cmd.Parameters.AddWithValue("@FileName", filename);
        cmd.Parameters.AddWithValue("@FilePath", "Documents/" + filename);

        con.Open();

        cmd.ExecuteNonQuery();
        con.Close();

        //Save file to project
        AjaxFileUpload1.SaveAs(@strDestPath + filename);
    }
    catch (Exception er) 
    { 
        Response.Write("<script language='javascript'> alert(' problem');</script>");   
    }
    finally { con.Close(); }     
}

web.config web.config中

<system.web>
    <compilation debug="true" targetFramework="4.5">
        <assemblies>
            <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </assemblies>
    </compilation>
    <httpHandlers>
        <add verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/>
    </httpHandlers>
    <httpRuntime targetFramework="4.5"/>
</system.web>

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
        <add name="AjaxFileUploadHandler" verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/>
    </handlers>
</system.webServer>

Any solution ? 有什么办法吗?

My work around: I created a data attribute named "data-upload-type" on ALL AjaxFileUpload controls and set it to the name of the type (upload type I use to know how to process the files). 我的变通方法:我在所有AjaxFileUpload控件上创建了一个名为“ data-upload-type”的数据属性,并将其设置为类型的名称(用于了解如何处理文件的上载类型)。 Then I set up the client call to grab that value and set a cookie with the same value. 然后,我设置客户端调用以获取该值,并设置具有相同值的cookie。 The cookie IS received on the server side functions and I branch based on the value I receive. Cookie是在服务器端函数上接收的,我根据接收到的值进行分支。

Here is an example: This is my common client start upload: function StartUpload(sender, args) { var t = $(sender._element).attr('data-upload-type'); document.cookie = 'upload-type=' + $(sender._element).attr('data-upload-type') + ';'; } 这是一个示例:这是我常见的客户端开始上传: function StartUpload(sender, args) { var t = $(sender._element).attr('data-upload-type'); document.cookie = 'upload-type=' + $(sender._element).attr('data-upload-type') + ';'; } function StartUpload(sender, args) { var t = $(sender._element).attr('data-upload-type'); document.cookie = 'upload-type=' + $(sender._element).attr('data-upload-type') + ';'; } function StartUpload(sender, args) { var t = $(sender._element).attr('data-upload-type'); document.cookie = 'upload-type=' + $(sender._element).attr('data-upload-type') + ';'; } My page code dropping one of the ajaxcontrols: <asp:AjaxFileUpload ID="afuUploader1" runat="server" OnClientUploadStart="StartUpload" OnUploadComplete="UploadComplete" OnClientUploadComplete="UploadComplete" data-upload-type="UploadType2" / > function StartUpload(sender, args) { var t = $(sender._element).attr('data-upload-type'); document.cookie = 'upload-type=' + $(sender._element).attr('data-upload-type') + ';'; }我的页面代码删除了一个ajaxcontrols: <asp:AjaxFileUpload ID="afuUploader1" runat="server" OnClientUploadStart="StartUpload" OnUploadComplete="UploadComplete" OnClientUploadComplete="UploadComplete" data-upload-type="UploadType2" / >

Then in your server side upload call, simply check Response.Cookies("upload-type"). 然后,在您的服务器端上传调用中,只需检查Response.Cookies(“ upload-type”)。 Works like a charm! 奇迹般有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在AjaxFileUpload onuploadComplete事件中获取选定的dropdownvalue? - How to get selected dropdownvalue in AjaxFileUpload onuploadComplete event? 来自用户控件ASP.NET的AjaxFileUpload回发事件 - AjaxFileUpload postback event from a usercontrol ASP.NET AjaxFileUpload OnClientUploadComplete和OnUploadComplete - AjaxFileUpload OnClientUploadComplete & OnUploadComplete 无法使用javascript和asp.net从AjaxFileUpload获取文件名 - Cannot get filename from AjaxFileUpload using javascript and asp.net 如何在AjaxFileUpload ASP.NET之后更新数据库表 - How to Update Database Table after AjaxFileUpload ASP.NET 如何使用asp.net获取使用ajaxfileupload上传的图像宽度和高度 - How to get image width and height uploaded using ajaxfileupload asp.net 无法更新在Firefox浏览器上使用AjaxFileUpload控件的asp.net WebForm中的标签 - Unable to update label in an asp.net WebForm which uses AjaxFileUpload control on Firefox browser 使用ASP.NET 4.0的ajaxcontroltoolkit查看错误ajaxfileupload数据导入和更新面板 - Seeing the error with ajaxcontroltoolkit ajaxfileupload data import and update panel for ASP.NET 4.0 如何使用AjaxFileUpload.js将HTML输入文件传递到C#ASP.Net? - How do you pass HTML input files to C# ASP.Net using AjaxFileUpload.js? GridView中的ajaxFileUpload不调用VB.NET事件 - ajaxFileUpload in a gridview not calling VB.NET Event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM