Upload súboru na web server spigi - 5. 11. 2002 21:02 - 10093 views Klasický problém pre začinajúcich programátorov webovských aplikácií. Tu je riešenie v ASP.NET. Ako prvé si vo Visual Studiu pridáme novú položku WebForm. Do stránky .aspx vložíme následujúci kód<%...@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Upload.WebForm1" %><html><head> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR" /> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE" /> <meta content="JavaScript" name="vs_defaultClientScript" /> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema" /></head><body><form id="Form1" method="post" enctype="multipart/form-data" runat="server"> <input id="subor" type="file" runat="server" name="subor" /> <asp:button id="tlacitko" runat="server" text="Upload"></asp:button></form></body></html>a do súboru WebForm1.aspx.vb vložíme následujúci kód:Public Class WebForm1Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents tlacitko As System.Web.UI.WebControls.Button Protected WithEvents subor As System.Web.UI.HtmlControls.HtmlInputFileWeb Form Designer Generated Code#Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent()Sub InitializeComponent() End Sub Private()Sub Private Sub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region()Sub#End Region Private Sub tlacitko_Click()Sub tlacitko_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlacitko.Click End Sub End Class()SubEnd ClassA teraz sú dve možnosti: - buď chceme súbor priamo uložiť na web server na disk. V tom prípade do procedúry tlacitko_Click() vložíme kód:If Not subor.PostedFile Is Nothing Then subor.PostedFile.SaveAs("c:\MyUploadFile.doc")End If- alebo potrebujeme so súborom spraviť niečo iné, napr. vložiť do databázy a podobne, tak môžeme získať súbor ďalším spôsobom a následne ďalej spracovať. Do tlacitko_Click() vložíme kód:Dim r As New System.IO.StreamReader(subor.PostedFile.InputStream())Dim _stringBuffer As String = r.ReadToEnd ' tu môžeme ďalej spracovať _stringBufferr.Close()