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

  • Home
  • SEARCH
  • 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 8179429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:59:50+00:00 2026-06-06T23:59:50+00:00

I am trying to implement a simple file upload, but having some troubles. When

  • 0

I am trying to implement a simple file upload, but having some troubles. When I hard-code the path it works fine. But for some reason, when I try to use a file upload, the controller name is being appended to the path

Hard coded path (what I’m trying to get):

@"C:\Users\Scott\Documents\The Business\MasterSpinSite\MasterSpin\MasterSpin\LOADME.txt"

Path I am getting an exception with (notice the “appz” controller name):

C:\Users\Scott\Documents\The Business\MasterSpinSite\MasterSpin\MasterSpin\appz\LOADME.txt'

My Controller

public ActionResult Load(spinnerValidation theData, HttpPostedFileBase file)
    {

        if (file.ContentLength > 0)
        {

            string filePath = Request.MapPath(file.FileName);

            string input = System.IO.File.ReadAllText(filePath);
            string[] lines = Regex.Split(input, "#!#");
           // ...... do stuff

        }

My View

<form action="" method="post" enctype="multipart/form-data">

  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />

<input type="submit" value="LOAD ME!">

 </form>

What could be causing this behavior ?

  • 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-06-06T23:59:51+00:00Added an answer on June 6, 2026 at 11:59 pm

    try this:

    public ActionResult Load(spinnerValidation theData, HttpPostedFileBase file)
    
        if (file.ContentLength > 0)
        {
            var filePath = System.IO.Path.GetFileName(file.FileName);
            using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
            {
                var input = sr.ReadToEnd();
                var lines = Regex.Split(input, "#!#");
            }
        }
    }
    

    (bug) System.IO.Path.GetFileName(file.FileName) return the name of file

    Edit

    change System.IO.Path.GetFileName(file.FileName) for Server.MapPath(file.FileName)

    public ActionResult Load(spinnerValidation theData, HttpPostedFileBase file)
    
        if (file.ContentLength > 0)
        {
            var filePath = Server.MapPath(file.FileName);
            using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
            {
                var input = sr.ReadToEnd();
                var lines = Regex.Split(input, "#!#");
            }
        }
    }
    

    Edit II

    or copy to diferent path:

    public ActionResult Load(spinnerValidation theData, HttpPostedFileBase file)
    
        if (file.ContentLength > 0)
        {
            var fileName = System.IO.Path.GetFileName(file.FileName);
            var fileUpload = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(fileUpload);
            if (System.IO.File.Exists(fileUpload)) 
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(fileUpload))
                {
                    var input = sr.ReadToEnd();
                    var lines = Regex.Split(input, "#!#");
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement some simple caching into my application and I'm having some
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
I am trying to implement a simple code tester : a very basic version
I'm trying to implement the simple-linkedinphp library (it's found at http://code.google.com/p/simple-linkedinphp/ ) It runs
I am trying to implement file upload functionality on my site. I have created
I'm trying to implement this method of making a nice form upload: www.kavoir.com/2009/02/styling-file-upload-select-input-control-input-typefile.html/ which
I am trying to implement Gmail style drag-and-drop file upload in ASP.NET MVC. I
I am trying to implement a very simple file transfer client in python using
I've been trying to implement what should be a pretty simple feature, but I'm
I am trying to implement file upload with jqgrid (in a Zend Framework project).

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.