Have a question? ask www.kenlet.com
Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  General ASP.NET >  Uploading files in ASP.NET
Add to MyHood
Uploading files in ASP.NET   [ printer friendly ]
Stats
  Rating: 1.92 out of 5 by 13 users
  Submitted: 02/13/02
Edwin Javier ()

 
Overview
Not so long ago uploading a file from a client browser to the server was a quite tedious task. Developers often had to use third party tools or write their own file upload utilities. With the development of ASP.NET writing a file upload script is a piece of cake! This example will illustrate how this is done. It will display an HTML form from which the user will select a file to upload. Once the user clicks the upload button, properties like its size, source and destination names are displayed.

HTML Form
The following form will accept a file name and then we will need submit to an aspx file:

<form runat="server" enctype="multipart/form-data">
<table>
<tr>
<td>Select File :</td>
<td><input type="file" id=file1 runat="server" /></td>
</tr>
<tr>
<class=tagname>td><asp:Button id=btn1 runat="server" text="Upload" 
onclick="upload"/>
</td>
</tr>
</table>
</form>


Functionality Code


                            

                                  
  <%@ page language="vb" %>
  <%@ import namespace="System" %>
  <%@ import namespace="System.Web" %>
<script language="vb" runat="server" > public sub page_load(s as object,e as eventargs) response.write ("<h1>devhood.com - File Upload ASP.NET Way...</h1>") end sub public sub upload(s as object,e as eventargs) dim s1 as string dim s2 as string dim pos as integer s1=file1.postedfile.filename pos=s1.lastindexof("\") +1 s2=s1.substring(pos) file1.postedfile.saveas (request.servervariables ("APPL_PHYSICAL_PATH") & "general\" & s2) response.write("<strong>File has been uploaded as " & request.servervariables("APPL_PHYSICAL_PATH") & s2 & " !</strong>") response.write("<h4>Client File Name : " & file1.postedfile.filename & "</h4>") response.write("<h4>File Size : " & file1.postedfile.contentlength & "</h4>") response.write("<h4>Content Type : " & file1.postedfile.contenttype & "</h4>") end sub </script> <% if page.ispostback=false then %> <form runat="server" enctype="multipart/form-data"> <table> <tr> <td>Select File :</td> <td><input type="file" id=file1 runat="server" /></td> </tr> <tr> <class=tagname>td><asp:Button id=btn1 runat="server" text="Upload" onclick="upload"/> </td> </tr> </table> </form> <% end if %>


Note that ENCTYPE type attribute is set to "multipart/form-data"; also the control is set to run at the server side.

Return to Browsing Tutorials

Email this Tutorial to a Friend

Rate this Content:  
low quality  1 2 3 4 5  high quality

Reader's Comments Post a Comment
 
functionally this code works (i didn't try it myself..I'm assuming) -- but you didn't comment what was happening and why... you just posted code with an intro of what it is... Secondly, you've missed out a lot of benefits of ASP.NET, such as clean code... without <% if %> stuck in the middle of HTML code... use of object visibility would help clean that up...as well as you've placed a lot of "response writes" which also make the code seem more messy and harder to follow.
-- Anonymous Person, February 13, 2002
 
no good
-- manuel torres, February 14, 2002
 
Comments and explanations about more of the code would be useful.
-- Reid Jonasson, February 15, 2002
 
Better commenting please
-- Adnan Rangwala, February 15, 2002
 
I don't really understand your code. Perhaps more elaboration or something in comments.
-- Bryan Chen, February 17, 2002
 
Not a very helpful tutorial.
-- Brian Simoneau, February 18, 2002
 
I agree...there needs to be more comments.
-- R P, February 25, 2002
 
This should be a linked tutorial, this was taken from BipinJoshi.com he just removed the guys name and replaced it with devhood.com

("<h1>BipinJoshi.com - File Upload ASP.NET Way...</h1>")
-- Ranjit Annamalai, March 07, 2002
 
Ouch.. that's horrible that he just took the tutorial and pasted it with the author's name. There is absolutely no explanation. If maybe you took his code and you wanted to comment each line and teach what it exactly does and functions, it'd be better.
-- Victor Vuong, March 08, 2002
 
This is definitely not good.
I have no idea what's happening in your codes.
-- Yusno Yunos, March 10, 2002
 
That sucked!
-- Sean Fitzgerald, March 14, 2002
 
1 for presentation, 5 for the contents/issue. Much better than ASP 3.0!!!
-- Radu Grama, July 10, 2002
 
more comments are needed
-- Kyoungjin Park, November 05, 2002
 
The worst How To I have seen in a while. Nothing even runs "upload" in the page load event.
-- Geoff Webb, June 12, 2003
 
PLease Add the code

(enctype = "multipart/form-data") is very inportant for this code to run

it should look like this:

<form id ="form1" enctype = "multipart/form-data" runat="server">

</form>
-- Raj Raj, November 13, 2003
 
Copyright © 2001 DevHood® All Rights Reserved