Simple upload/download form in ASP.NET MVC

Question!

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?



Answers

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.



This video can help you solving your question :)
By: admin