I am attempting to let a client upload a file to my server in an ASP.NET C# website. I have created a simple script to introduce myself to the nuances of both uploading a file and also creating a script where all the code (HTML and C# code) is in the same file.
My Problem: The function uploadFile() never outputs/returns any string result when it should return a string describing if the upload succeeded or why it failed.
What am I doing wrong?
PS: I have updated my web.config with the following code:
<appSettings>
<add key="folderPath" value="uploadedFiles"></add>
</appSettings>
PPS: Is there anything wrong with having my C# code & HTML all in the same .aspx file? Is it better to separate them out?
My Simple Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication1.test" %>
<!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 runat="server">
<title></title>
<script type="text/C#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
protected global::System.Web.UI.HtmlControls.HtmlInputFile fileUpload;
protected global::System.Web.UI.WebControls.Label lblMessage;
protected global::System.Web.UI.WebControls.Button btnSave;
protected global::System.Web.UI.HtmlControls.HtmlGenericControl test;
protected void bt1Clicked(Object sender, EventArgs e)
{
if (fileUpload.PostedFile != null)
{
string strFilename, strMessage;
strFilename = fileUpload.PostedFile.FileName.ToString();
strMessage = uploadFile(strFilename, ConfigurationManager.AppSettings["folderPath"]);
output.InnerHtml = strMessage;
}
}
public string uploadFile(string fileName, string folderName)
{
if (fileName == "")
{
return "Invalid filename supplied";
}
if (fileUpload.PostedFile.ContentLength == 0)
{
return "Invalid file content";
}
fileName = System.IO.Path.GetFileName(fileName);
if (folderName == "")
{
return "Path not found";
}
try
{
if (fileUpload.PostedFile.ContentLength <= 2048000)
{
fileUpload.PostedFile.SaveAs(Server.MapPath(folderName) + "\\" + fileName);
return "File uploaded successfully";
}
else
{
return "Unable to upload,file exceeds maximum limit";
}
}
catch (UnauthorizedAccessException ex)
{
return ex.Message + "Permission to upload file denied";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="fileUpload" type="file" Runat="server" NAME="fileUpload"/>
<asp:button id="btnSave" runat="server" Text="Upload File" OnClick="bt1Clicked"></asp:button>
<p id="output" runat="server">Result should be displayed here after clicking</p>
</div>
</form>
</body>
</html>
If you’re going to put your C# code in the front page, you’ll need to put it in a
<script runat="server">tag. When I do this (which is very rarely), I’ll usually put the<script>tag after the HTML. For example:The advantage of using
<script runat="server">is that if you are writing a Web Application (where the code compiles into a dll), you can create pages that run server side without doing a new build of the application (of course you’ll have to remove theCodeBehindattribute from the@Pagedirective). So if you’re in a situation where you can’t do a new build for whatever reason, writing everything in the.aspxfor a one off page can be useful.That being said, you’re probably better off writing the code in the codebehind file (In your case this is
test.aspx.cs, and you can get to it in VS.NET by right clicking ontest.aspxin the Solution Explorer and selecting “View Code” or by pressing F7 when you’re viewing thetest.aspxfile). Using the codebehind file separates the code from the HTML, and more importantly compilation errors will be raised when you compile the application. If you use<script runat="server">, compilation errors won’t be raised until the page is accessed.