Hi,
For uploading files to server using the new FileUpload control in C#,
<%@ Page Language="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 have not specified a file.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>FileUpload Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" Runat="server" />
<asp:Button ID="Button1" Runat="server" Text="Upload"
OnClick="Button1_Click" />
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
</html>
Hi,
For uploading files to server in C#/C Sharp,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="UploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="Uploader" runat="server" Height="24px" Width="472px" />
<asp:Button ID="cmdUpload" runat="server" Height="24px" OnClick="cmdUpload_Click"
Text="Upload" Width="88px" /><br />
<br />
<asp:Label ID="lblInfo" runat="server" EnableViewState="False" Font-Bold="True"></asp:Label></div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class UploadFile : System.Web.UI.Page
{
private string uploadDirectory;
protected void Page_Load(object sender, EventArgs e)
{
uploadDirectory = Path.Combine(Request.PhysicalApplicationPath, "Uploads");
}
protected void cmdUpload_Click(object sender, EventArgs e)
{
if (Uploader.PostedFile.FileName == "")
{
lblInfo.Text = "No file specified.";
}
else
{
string extension = Path.GetExtension(Uploader.PostedFile.FileName);
switch (extension.ToLower())
{
case ".png":
case ".jpg":
break;
default:
lblInfo.Text = "This file type is not allowed.";
return;
}
string serverFileName = Path.GetFileName(Uploader.PostedFile.FileName);
string fullUploadPath = Path.Combine(uploadDirectory,serverFileName);
try
{
Uploader.PostedFile.SaveAs(fullUploadPath);
lblInfo.Text = "File " + serverFileName;
lblInfo.Text += " uploaded successfully to ";
lblInfo.Text += fullUploadPath;
}
catch (Exception err)
{
lblInfo.Text = err.Message;
}
}
}
}
Hi,
Follow Us on Google Plus & +1 your favorite posts.
Coderz Heaven Google Plus Page - https://plus.google.com/107741547017453402037
Keep coding… Keep in touch…