Asking the user to upload
a file to the server is a very very common task in
most of the web applications. In classic ASP, uploading file was a very
difficult task, since we need a third party component or we need to write our
component to upload file from client to the server. In ASP .NET Uploading a file from client to the web server is so easy.
To be precise, we can get this done in two lines of code. Yes, two lines of
code. In this article, we will be looking into the following:
- How to Upload a file from
Client to the Web server in ASP .NET?
First of all, we need a
HTML server control to allow the user to select the file. This is nothing but
the same old <input tag, with the type set to File, such as <input
type=file id=myFile runat=server
/>. This will give you the textbox and a browse button. Once you have
this, the user can select any file from their computer (or even from a
network). Then, in the Server side, we need the following line to save the file
to the Web Server.
myFile.PostedFile.SaveAs("DestinationPath")
<html>
|
In the above example, you
should note that, in the FORM tag, we have set the ectype to "multipart/form-data".
This is another important aspect while uploading a file to the web server. We
have a Input
tag which is of type file. Then we have a button called
"Upload". On the onclick of the button, you
can see that, we are using some string functions to find the FileName (excluding the path). Finally, we invoke the
method SaveAs
to save the file (upload) in the web server.
0 comments:
Post a Comment