ASP.NET上传文件控件并显示等待消息

时间:2018-11-14 09:52:30

标签: asp.net vb.net file-upload

我已经在互联网上搜索并尝试了不同的方法,但是我很难使它工作。我将接受任何帮助,包括第三方组件,javascript和所有内容... 我的问题(我认为)是我没有使用“上传”按钮,而是使用了一个还可以保存数据表单的按钮。 标记:

<div id="divInserimento" runat="server" class="contact-form scrollDiv">
    <div>
        <asp:TextBox runat="server" ID="txtTitolo" placeholder="Titolo nota" CssClass="input_text" MaxLength="1000"></asp:TextBox>
    </div>
    <div>
        <asp:TextBox runat="server" ID="txtNota" placeholder="Nota" CssClass="input_text" TextMode="MultiLine" Height="10em"></asp:TextBox>
    </div>

    <div style="padding: 3px; background-color: whitesmoke; border: 1px solid black;">
        <asp:FileUpload runat="server" ID="MyFileUpload" Width="100%" />
        <asp:HiddenField runat="server" ID="hdnNomeFile" />
    </div>
    <p>&nbsp;</p>
    <table border="0" cellspacing="0" cellpadding="0" style="width: 100%">
        <tr>
            <td align="left">
                <asp:Button runat="server" CssClass="gbutton" ID="btnAnnullaIns" Text="Annulla" UseSubmitBehavior="false" OnClick="btnAnnullaIns_Click" />
            </td>
            <td align="right">
                <asp:Button runat="server" CssClass="gbutton" ID="btnConfermaIns" Text="Conferma" OnClick="btnConfermaIns_Click" UseSubmitBehavior="false" />
            </td>
        </tr>
    </table>
</div>

后面的代码:

Function Upload()
        Dim file As String = ""
        If MyFileUpload.HasFile Then
            If MyFileUpload.PostedFile.ContentLength <= 15000000 Then

                'throbber.Style("display") = "normal"
                Dim path As String = My.Settings.pathUpload1 & rfCliente & My.Settings.pathUpload2
                If Not Directory.Exists(path) Then
                    'If Directory (Folder) does not exists. Create it.
                    Directory.CreateDirectory(path)
                End If

                file = path & MyFileUpload.FileName

                MyFileUpload.SaveAs(file)
                hdnNomeFile.Value = file
                ClientScript.RegisterStartupScript(Me.GetType(), "Scriptkey", String.Format("alert('{0}');", "File caricato: " & MyFileUpload.FileName), True)

                If MyFileUpload.FileName.Contains(".txt") Then
                    Dim srRead As New System.IO.StreamReader(file)
                    Dim strFileText As String = ""
                    strFileText = srRead.ReadToEnd
                    srRead.Close()
                    txtNota.Text = strFileText
                    txtTitolo.Text = MyFileUpload.FileName
                End If
                Return True
            Else
                hdnNomeFile.Value = ""
                ClientScript.RegisterStartupScript(Me.GetType(), "Scriptkey", String.Format("alert('{0}');", "Errore nel caricamento"), True)
                Return False
            End If
            'throbber.Style("display") = "none"

        Else
            ClientScript.RegisterStartupScript(Me.GetType(), "Scriptkey", String.Format("alert('{0}');", "Il file supera le dimensioni massime consentite (15mb)"), True)
            Return False
        End If
    End Function

按下btnConfermaIns按钮可以调用“上传”功能,如下所示:

If MyFileUpload.HasFile Then
                If Upload() Then
                    inserimentoNota()
                Else
                    ClientScript.RegisterStartupScript(Me.GetType(), "Scriptkey", String.Format("alert('{0}');", "Errore nel caricamento"), True)
                End If
            Else
                inserimentoNota()
            End If

谢谢大家

1 个答案:

答案 0 :(得分:1)

如果我正确理解,您想在文件上传时显示“等待”消息。

您可以使用javascript完成此操作:

<asp:Button runat="server" CssClass="gbutton" ID="btnConfermaIns" Text="Conferma" OnClick="btnConfermaIns_Click" UseSubmitBehavior="false" OnClientClick="showWaitingMessage()" />
<div id="waitMessage" style="display: none;">Please wait...</div>
<script>
     function showWaitingMessage() {
          document.getElementById("waitMessage").style.display = "block";
     }
</script>