I heard that uploading is already in ASP.NET?
Anyone else know this? And knows how to do it?
Printable View
I heard that uploading is already in ASP.NET?
Anyone else know this? And knows how to do it?
Modified(C# to VB) code sample I **believe** I got from www.aspalliance.com. Only allows GIF or JPG but can be easily modified. Allows upto 5 files to be uploaded at one time. Obviously, the paths for saving the image must be changed to your configuration.
John
Code:<%@ Page Language="vb" %>
<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Me.IsPostBack) Then Me.SaveImages()
End Sub
Private Function SaveImages() As System.Boolean
'//loop through the files uploaded
Dim _files As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
'//Message to the user
Dim _message As New System.Text.StringBuilder("Files Uploaded:<br><br>")
Dim _iFile As System.Int32
Try
For _iFile = 0 To _files.Count - 1
'// Check to make sure the uploaded file is a jpg or gif
Dim _postedFile As System.Web.HttpPostedFile = _files(_iFile)
Dim _fileName, _fileExtension As System.String
_fileName = System.IO.Path.GetFileName(_postedFile.FileName)
If _fileName <> "" Then
_fileExtension = System.IO.Path.GetExtension(_fileName)
If (_fileExtension = ".gif") Then
'//Save File to the proper directory
_postedFile.SaveAs("\images\GIF\" + _fileName)
_message.Append(_fileName + " successfully uploaded.<BR>")
ElseIf (_fileExtension = ".jpg") Then
'//Save File to the proper directory
_postedFile.SaveAs("\images\JPG\" + _fileName)
_message.Append(_fileName + " successfully uploaded.<BR>")
Else
_message.Append(_fileName & " <font color=""red"">failed!! Only .gif and .jpg images allowed!</font> <BR>")
End If
End If
Next
Label1.Text = _message.ToString()
Return True
Catch Ex As System.Exception
Label1.Text = Ex.Message
Return False
End Try
End Function
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>::: UPLOAD SAMPLE ::: </title>
</HEAD>
<body>
<center>
<form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data">
<h3>Multiple File Upload Example</h3>
<P>
<INPUT type="file" runat="server" size="50" ID="File1" NAME="File1"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File2" NAME="File2"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File3" NAME="File3"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File4" NAME="File4"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File5" NAME="File5"></P>
<P><STRONG>:: </STRONG>
<asp:LinkButton id="LinkButton1" runat="server" Font-Names="Verdana" Font-Bold="True" Font-Size="XX-Small">Upload Images</asp:LinkButton> <STRONG>::
</STRONG> <A href="JavaScript:document.forms[0].reset()" id="LinkButton2" style="FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana">
Reset Form</A> <STRONG>::</STRONG></P>
<P>
<asp:Label id="Label1" runat="server" Font-Names="verdana" Font-Bold="True" Font-Size="XX-Small" Width="400px" BorderStyle="None" BorderColor="White"></asp:Label></P>
<P> </P>
</form>
</center>
</body>
</HTML>
Patoooey
Nice example,
I have done a similar thing on a site we use, but also allows you to navigate a directory tree to select the upload folder (limited of course) required, create sub folders, and upload other non executable file formats. Once the upload has succeeded then the file properties are displayed in a status area of the screen.
Thanks:)
what am I doing wrong? I can't get it to work. All I ever get is a 0 count on the ofiles.Count line.
Code:Public Class upload
Inherits System.Web.UI.Page
Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If IsPostBack = True Then Upload_File()
End Sub
Sub Upload_File()
Dim ofiles As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
If ofiles.Count > 0 Then
Dim HPFpostedFile As System.Web.HttpPostedFile = ofiles(1)
Dim sfileName As String
sfileName = System.IO.Path.GetFileName(HPFpostedFile.FileName)
'//Save File to the proper directory
HPFpostedFile.SaveAs("\reg\" + sfileName)
Response.Write("IT FREAKING WORKED!")
Else
Response.Write("Not working")
End If
End Sub
End Class
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="upload.aspx.vb" Inherits="DartS.upload"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>upload</title>
<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 MS_POSITIONING="GridLayout">
<form id="UPLOAD" method="post" runat="server">
<asp:button id="Button1" style="Z-INDEX: 101; LEFT: 36px; POSITION: absolute; TOP: 271px" runat="server" Text="Button"></asp:button>
<INPUT id="File1" style="Z-INDEX: 103; LEFT: 30px; POSITION: absolute; TOP: 218px" type="file" name="File1" runat="server">
</form>
</body>
</HTML>
I figured it out..
was missing the enctype thing..Code:<form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data">
Now that i have it figued out, here is a real simple way to do it:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
fSend.PostedFile.SaveAs("C:\" & System.IO.Path.GetFileName(fSend.PostedFile.FileName))
End Sub
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="upload.aspx.vb" Inherits="DartS.upload"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>upload</title>
<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 MS_POSITIONING="GridLayout">
<form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data">
<asp:button id="Button1" style="Z-INDEX: 101; LEFT: 36px; POSITION: absolute; TOP: 271px"
runat="server" Text="Button"></asp:button>
<INPUT id="fSend" style="Z-INDEX: 103; LEFT: 30px; POSITION: absolute; TOP: 218px"
type="file" name="File1" runat="server">
</form>
</body>
</HTML>