|
Upload file using .NET
With Classic ASP it was difficult to upload
files to your account via a web page without
the use of a third party component. This
has changed with the introduction of .NET
and it's now possible to easily upload files
with just a few lines of code.
The following script is a generic upload
script that uses an html form which allows
you to select a file on your local machine
and then upload it to the server - the file
will be uploaded to the some folder on the
server from which the upload script was
run.
Just copy and paste the code into a file
and save it to your account with an .aspx
suffix.
<html>
<script language="VB" runat="server">
Sub File_Upload (Sender as Object, e as
EventArgs)
Dim strFileName as string
strFileName = MyFile.PostedFile.FileName
Dim myFileName as string = System.IO.Path.GetFileName(strFileName)
Try
MyFile.PostedFile.SaveAs(Server.mappath("/")
+ myFileName)
FileName.InnerHtml = MyFile.PostedFile.FileName
FileContent.InnerHtml = MyFile.PostedFile.ContentType
FileSize.InnerHtml = MyFile.PostedFile.ContentLength
ThankYouMessage.Visible = true
catch Exp as exception
ErrorMessage.Visible = true
End Try
End Sub
</Script>
<body>
<form method="Post" enctype="Multipart/Form-Data"
runat="Server">
Choose a file to upload to the server:<br>
<input id="MyFile" type="File"
runat="Server" size="25">
<p></p>
<input type="Submit" value="Upload"
onserverclick="File_Upload" runat="Server"><P>
<asp:panel id="ThankYouMessage"
runat="server" Visible="False">
Your File was sucessfull.<p>
File Name: <span id="FileName"
runat="Server"/><br>
File Content: <span id="FileContent"
runat="Server"/><br>
File Size: <span id="FileSize"
runat="Server"/>bytes<br>
</asp:panel>
<asp:panel id="ErrorMessage"
runat="server" Visible="False">
Your File was not sucessful.
</asp:panel>
</form>
</body>
</html>
|