【发布时间】:2019-04-17 05:57:28
【问题描述】:
我已经在互联网上搜索并尝试了不同的方法,但我很难让它发挥作用。我会接受任何帮助,包括第三方组件、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> </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
谢谢大家
【问题讨论】:
标签: asp.net vb.net file-upload