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 427575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:35:24+00:00 2026-05-12T19:35:24+00:00

I’m getting started with ASP.NET MVC, but would like to explore further. I’ve walked

  • 0

I’m getting started with ASP.NET MVC, but would like to explore further. I’ve walked through the usual “hello, world” examples, and now I’d like to try something a little more complex. Specifically, I want to be able to:

  • upload some XML file,
  • do some simple processing on it, and then
  • allow the user to download a new file with the results of the processing

In this case, the processing I’d like to do is something trivial that proves that I really got the XML file and have examined it. For example, counting the number of <basket> elements is sufficient. After I do the processing, I’d like to stream the results as a new file to the user (say, a text file that just contains the sentence “There were 398 <basket> elements”) so that their browser will initiate a download.

What’s a good general approach for this?

  • 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-12T19:35:25+00:00Added an answer on May 12, 2026 at 7:35 pm

    To do the upload, you’ll need to have a file input and a form that uses an enctype of multipart/form-data. On the server you get an HttpPostedFileBase object from the Request.Files collection element matching your input’s tag name. Then you access the stream on the file object and read it.

    Once you have the data, you perform your transformation — here you are reading the count of a particular tag. Then you want to return a FileResult from your action. Since it’s really just a string, I’d suggest writing it to a MemoryStream, then rewinding that string and creating a FileResult from it.

    <% using (Html.BeginForm("Upload","Controller",FormMethod.Post, new { enctype = "multipart/form-data"))
       { %>
         <label for="uploadFile">File:</label>
         <input type="file" name="uploadFile" id="uploadFile" />
         <input type="submit" value="Upload" />
    <% } %>
    

    Code — since I assume you’ll eventually want to do something more complicated, I’ll include some (untested/uncompiled) code that does what you described (I think).

    public ActionResult Upload()
    {
        var file = Request.Files["uploadFile"];
        if (file == null)
        {
            ModelState.AddModelError( "uploadFile", "No file specified" );
            return View();
        }
    
        var reader = new StreamReader( file.InputStream );
    
        var doc = XDocument.Load( reader );
    
        var count = doc.Descendants().Where( n => n.Name == "basket" ).Count();
    
        var output = new MemoryStream();
        var writer = new StreamWriter( output );
    
        writer.Write( "{0} baskets", count );
        output.Seek( 0, SeekOrigin.Begin );
    
        return File( output, "text/plain", "count.txt" );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.