File uploads

Create new web site

Select ASP.Net WebSite, and  the following files will be created as shown below.
Intranet users: FYI, the virtual path is http://manas6/aspnet.35/mm.richcont2/ and pull path is C:\inetpub\wwwroot\aspnet.35\mm.richcont2

 

If you are using IIS, you may use all the admin GUIto help yourself.

Create a new folder

Now, design your page as shown below or use the code (Note clean Rebuild)

 

Runtime analysis

Browse to the source file.

file mapped

There was an error, as shown below due this faulty code

try
{
//FileUpload1.SaveAs("C:\\Uploads\\" + FileUpload1.FileName);
String str_path = "-/upload/" + FileUpload1.FileName;
// FileUpload1.SaveAs(MapPath(str_path) + FileUpload1.FileName);
FileUpload1.SaveAs(MapPath(str_path));

Corrected, if you are using IIS, and may face this error show below; meaning you can't access this folder due to security setup.

try
{
//FileUpload1.SaveAs("C:\\Uploads\\" + FileUpload1.FileName);
String str_path = "~/upload/" + FileUpload1.FileName;
// FileUpload1.SaveAs(MapPath(str_path) + FileUpload1.FileName);
FileUpload1.SaveAs(MapPath(str_path));

To fix this follow this step

Now get to  the property of upload folder using mouse right click.

does read only blocks,

Now get back to the page and refresh

Now if want to view the files, you may explore to test, since if have few images associated with this page you may like to add those.

Adding Images

Confirmations of adding chic.png, now add all the images.

Now you may like add a link to open this page.

 

Code Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>mm.richcont2 : Simple File Upload</title>
<style type="text/css">
body
{
background-color: #FFFFCC;
}
table
{
border-style:solid;
border-color:Navy;
border-width: 2px;
text-align:center;
}
.style2
{
width: 468px;
}
div
{
border-style:solid;
border-color:Navy;
border-width: 2px;
text-align: center;
width: 700px;
overflow:scroll;
background-color: #CCFFCC;
}
</style>

</head>
<body>
<form id="form1" runat="server">
<div>
<table><tr> <td><asp:Label id="L1" Text="File Source:" AssociatedControlID="FileUpload1" Runat="server" /></td>
<td class="style2">
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br /></td>
</tr><tr><td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="http://manas6/aspnet.35/mm.richcont2/upload/birds.htm">Birds</asp:HyperLink>
</td>
<td class="style2">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Upload File" />&nbsp;<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label></td></tr></table>
</div>
</form>

</body>
</html>
 

Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
try
{
//FileUpload1.SaveAs("C:\\Uploads\\" + FileUpload1.FileName);
String str_path = "~/upload/" + FileUpload1.FileName;
// FileUpload1.SaveAs(MapPath(str_path) + FileUpload1.FileName);
FileUpload1.SaveAs(MapPath(str_path));
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.";
}

}
void Page_PreRender()
{
String filepath = MapPath("-/upload/");
}

}