Upload
Upload.aspx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <%@ Control Language="vb" AutoEventWireup="false" Codebehind="upload.aspx.vb" Inherits="DamianM.upload" enableViewState="False" %> <form id="Form1" method="post" runat="server" enctype="multipart/form-data" action="/code/2/upload.aspx"> <center> <table border="0"> <tr> <td> Upload File </td> </tr> <tr> <td> <INPUT type="file" id="upload" name="upload" size="40"> <INPUT type="submit" value="Submit"> </td> </tr> <tr> <td> <font color="red"> <asp:Literal id="ErrorMsg" runat="server"></asp:Literal></font> </td> </tr> <tr> <td> File Contents </td> </tr> <tr> <td> <TEXTAREA rows="5" cols="50"><asp:Literal id="FileContents" runat="server"></asp:Literal></TEXTAREA> </td> </tr> <tr> <td> File Link </td> </tr> <tr> <td> <asp:Literal id="FileLink" runat="server"></asp:Literal> </td> </tr> </table> </center> </form> |
Upload.aspx.vb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | Imports System Imports System.Web Public Class upload Inherits System.Web.UI.UserControl Protected WithEvents ErrorMsg As System.Web.UI.WebControls.Literal Protected WithEvents FileLink As System.Web.UI.WebControls.Literal Protected WithEvents FileContents As System.Web.UI.WebControls.Literal Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim File As HttpPostedFile ErrorMsg.Text = "" FileLink.Text = "(none)" FileContents.Text = "" 'check that a file has been uploaded If Request.Files.Count <= 0 Then ErrorMsg.Text = "No file was uploaded" Else 'get the file data File = Request.Files.Get(0) 'check thats its not too big If File.ContentLength > 1024 Then ErrorMsg.Text = "File is bigger than 1k" Else If File.ContentType <> "text/plain" Then ErrorMsg.Text = "File is not a text file" Else 'save file File.SaveAs(Server.MapPath("..\..\files\upload\deleteme.txt")) 'show link FileLink.Text = "<a href=""../../files/upload/deleteme.txt"">" + File.FileName + "</a>" 'open in out stream Dim sr As System.IO.StreamReader Dim strContent As String sr = New System.IO.StreamReader(File.InputStream) 'display contents FileContents.Text = sr.ReadToEnd sr.Close() End If End If End If End Sub End Class |




















June 14th, 2007 at 7:22 pm
[...] Upload Source Code [...]