Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7583635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:43:43+00:00 2026-05-30T18:43:43+00:00

I am attempting to let a client upload a file to my server in

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:43:45+00:00Added an answer on May 30, 2026 at 6:43 pm

    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:

    <!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">
    ...
    </html>
    <script runat="server">
    //Code here
    </script>
    

    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 the CodeBehind attribute from the @Page directive). So if you’re in a situation where you can’t do a new build for whatever reason, writing everything in the .aspx for 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 on test.aspx in the Solution Explorer and selecting “View Code” or by pressing F7 when you’re viewing the test.aspx file). 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a line in an XML file like the following (derived
I've written a web service using ASP.NET (in C#) and I'm attempting to write
I'm attempting to learn Haskell by writing a SIP client. The question I have
Attempting to deploy a MOSS solution to a UAT server from dev server for
let me begin by stating that's i'm dreadful at math. i'm attempting to reposition
Ok, let me try to clearly explain what I'm attempting to accomplish here. Basically,
Let me start by saying I am not an SQL or SQL Server expert
let me first off noting that I have absolutely no idea what I'm doing
I am attempting to incorporate Windows Live ID in my application. First, I created
EDIT: Let me turn this into a straight SQL question... I have a varbinary(max)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.