隐藏字段值显示未定义

时间:2014-03-13 18:15:32

标签: asp.net ajax undefined hidden-field asyncfileupload

我有简单的asp.net异步上传,我试图从本地系统上传样本图像,并在上传时尝试执行简单的web方法。

Scenanrio:

  1. 使用异步上传器上传图片并将其正常工作并将其上传到dest文件夹。

  2. 现在尝试在隐藏字段中保存fileupload.filename并测试它是否真正获取文件名(这也有效)

  3. 现在尝试发送保存隐藏字段值的var时出现了问题。 这里显示我是未定义的。

  4. 这是我的代码:

    Protected Sub fileUploadComplete(ByVal sender As Object, ByVal e As AsyncFileUploadEventArgs)
        Thread.Sleep(1000)
        Dim filename As String = System.IO.Path.GetFileName(fileUpload1.FileName)
        fileUpload1.SaveAs(Server.MapPath("UploadFiles/") & RandomFolder.Value & "/" & filename)
        lblFilename.Value = filename
    End Sub
    
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CreateFolder()
        End If
        Dim mypath As String = Server.MapPath("~/UploadFiles/")
        myFilePath.Value = mypath
    
    End Sub
    <WebMethod()> _
    

        公共函数FileDetails(ByVal Filename As String,ByVal mypath As String)As String       //一些操作         返回“真实”     结束功能

    HTML:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
               <link href="style/bootstrap.css" rel="stylesheet" />
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script type="text/javascript">
        // This function will execute after file uploaded successfully
        function uploadComplete() {
            document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "File Uploaded Successfully";
    }
    // This function will execute if file upload fails
    function uploadError() {
        document.getElementById('<%=lblMsg.ClientID %>').innerHTML = "File upload Failed.";
    }
    
    $(document).ready(function () {
        $("#btnconvert").click(function () {
            var FileFolder = $('#RandomFolder').val();
            var filename = document.getElementById('<%=lblFilename.ClientID %>').val();
            alert(FileFolder);
            alert(filename);
            $.ajax({
                type: "POST",
                url: "AsyncUpload.aspx/FileDetails",
                data: JSON.stringify('{"Filename":"' + filename + '","mypath":"' + FileFolder + '"}'),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Replace the div's content with the page method's return.
                    alert("Success");
                }
            });
        });
    });
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:ToolkitScriptManager ID="scriptManager1" runat="server"/>
    <div>
    <asp:AsyncFileUpload ID="fileUpload1" OnClientUploadComplete="uploadComplete" OnClientUploadError="uploadError" 
    CompleteBackColor="White" Width="350px" runat="server" UploaderStyle="Modern" UploadingBackColor="#CCFFFF" 
    ThrobberID="imgLoad" OnUploadedComplete="fileUploadComplete" /><br />
    <asp:Image ID="imgLoad" runat="server" ImageUrl="loading.gif" />
    <br />
    <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
    </div>
        <asp:HiddenField ID="RandomFolder" runat="server"/>
        <asp:HiddenField ID="FilePath" runat="server"/>
         <asp:HiddenField ID="myFilePath" runat="server"/>
         <asp:HiddenField ID="lblFilename" runat="server"/>
         <button id="btndetails" class="btn btn-danger btnconvert" type="button">Convert file</button>
        </div>
    

2 个答案:

答案 0 :(得分:0)

您使用val()方法调用隐藏字段的值,但该字段对象上不存在该方法;它是一个jQuery方法。将您隐藏的字段包装在jQuery中:

var filename = $('#<%=lblFilename.ClientID %>').val();

或者只是在你的字段上调用.value属性:

var filename = document.getElementById('<%=lblFilename.ClientID %>').value;

答案 1 :(得分:0)

请参阅此page它可以帮助您实现正在尝试的目标