In ASP.NET you could upload files using the HTML FileUpload server control.This control put an <input type = " file " > element on your Web page to enable the end user to upload files to the server.To use the file ,however,you had to make a couple of modifications to the page.For example , you were required to add enctype = " multipart/form-data " to the page's <form> element.
ASP.NET introduces a new FileUpload server control that makes the process of uploading files to a server even simpler.When giving a page the capability to upload files ,you simplay include the new < asp : FileUpload > control and ASP.NET thake care of the rest,including adding the enctype attribute to the page's < form > element
The following code shows the Uploading files using the new FileUpload control
VB
<script runat = " server ">
protected Sub Button1_Click( ByVal sender As object , ByVal e As System.EventArgs )
if FileUpload1.hasFile Then
Try
FileUpload1.SaveAs (" C:\Uploads\" & _
FileUpload1.FileName )
Label1.Text = "File Name: " & _
FileUpload1.PostedFile.FileName & "<br>" & _
"File Size: " & _
FileUpload1.PostedFile.ContentLength & " kb<br> " & _
"Content type : " & _
FileUpload1.PostedFile.ContentType
Catch ex As Exception
Label1.Text = "ERROR: " & ex.Message.ToString( )
End Try
Else
Label1.Text = "You have not specified a file. "
End If
End Sub
</script>
< html xmlns = "http:/www.w3.org/1999/xhtml " >
< head runat = "server " >
< title > FileUpload Server control </ title >
</ head >
< body >
< form id = "form1" runat = "server" >
< asp : FileUpload ID = "FileUpload1" Runat = "server" />
< p >
< asp : Button ID = "Botton1" Runat = "server" Text = "Upload"
OnClick = "Button1_Click" />
< / p >< p >
< asp : Label ID = " Label1 " Runat = "server" ></ asp : Label ></ p >
</ form >
</ body >
</ html >
C#
< script runat = "server " >
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile )
{
try
{
FileUpload1.SaveAs( "C:\\Uploads\\" + FileUpload1.FileName);
Label1.Text = "File Name: " + FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + "kb<br>" + "Content Type: " +
FileUpload1.PostedFile.ContentType;
}
catch (Exception ex)
{
Label1.Text = "ERROR:" + ex.Message.ToString();
}
}
else
{
Label1.Text = "You hav not specified a file";
}
}
</ scripts >
ASP.NET introduces a new FileUpload server control that makes the process of uploading files to a server even simpler.When giving a page the capability to upload files ,you simplay include the new < asp : FileUpload > control and ASP.NET thake care of the rest,including adding the enctype attribute to the page's < form > element
The following code shows the Uploading files using the new FileUpload control
VB
<script runat = " server ">
protected Sub Button1_Click( ByVal sender As object , ByVal e As System.EventArgs )
if FileUpload1.hasFile Then
Try
FileUpload1.SaveAs (" C:\Uploads\" & _
FileUpload1.FileName )
Label1.Text = "File Name: " & _
FileUpload1.PostedFile.FileName & "<br>" & _
"File Size: " & _
FileUpload1.PostedFile.ContentLength & " kb<br> " & _
"Content type : " & _
FileUpload1.PostedFile.ContentType
Catch ex As Exception
Label1.Text = "ERROR: " & ex.Message.ToString( )
End Try
Else
Label1.Text = "You have not specified a file. "
End If
End Sub
</script>
< html xmlns = "http:/www.w3.org/1999/xhtml " >
< head runat = "server " >
< title > FileUpload Server control </ title >
</ head >
< body >
< form id = "form1" runat = "server" >
< asp : FileUpload ID = "FileUpload1" Runat = "server" />
< p >
< asp : Button ID = "Botton1" Runat = "server" Text = "Upload"
OnClick = "Button1_Click" />
< / p >< p >
< asp : Label ID = " Label1 " Runat = "server" ></ asp : Label ></ p >
</ form >
</ body >
</ html >
C#
< script runat = "server " >
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile )
{
try
{
FileUpload1.SaveAs( "C:\\Uploads\\" + FileUpload1.FileName);
Label1.Text = "File Name: " + FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + "kb<br>" + "Content Type: " +
FileUpload1.PostedFile.ContentType;
}
catch (Exception ex)
{
Label1.Text = "ERROR:" + ex.Message.ToString();
}
}
else
{
Label1.Text = "You hav not specified a file";
}
}
</ scripts >
No comments:
Post a Comment