|
-
Apr 29th, 2002, 11:30 AM
#1
Thread Starter
Fanatic Member
Inbuilt Upload?
I heard that uploading is already in ASP.NET?
Anyone else know this? And knows how to do it?
-
Apr 30th, 2002, 04:56 PM
#2
Fanatic Member
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>
-
May 1st, 2002, 05:01 AM
#3
Fanatic Member
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.
-
May 1st, 2002, 10:56 AM
#4
Thread Starter
Fanatic Member
Thanks
-
Aug 27th, 2002, 06:08 PM
#5
Addicted Member
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>
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Aug 27th, 2002, 06:54 PM
#6
Addicted Member
I figured it out..
Code:
<form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data">
was missing the enctype thing..
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Aug 27th, 2002, 07:04 PM
#7
Addicted Member
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>
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|