0

I am new to MVC and trying to figure out a way to do the below:

Basically, I have a URL http://dev.test.com/api/data/abczyz12345, which when clicked/opened from the UI shows the results as:

"email","f_name","l_name","c_name","ids" "abc1@test.com","C G","Wander","C1","" "abc2@Atest.COM","Virginia","Dale","A & D","" .... and so on....

On my view I display the above link as an anchor and when the user clicks on it - it gets opened on the same page. Is there a way that when the user clicks on the link - instead of opening it on the web page it opens as a CSV file (giving the user an option to download)?

dotNetNewbie
  • 759
  • 4
  • 17
  • 34
  • 2
    http://stackoverflow.com/questions/989927/recommended-way-to-create-an-actionresult-with-a-file-extension – Shyju Apr 25 '12 at 19:35

3 Answers3

1

Yes, you have to specify what type of file it is using the HTTP headers. Here's an example using PHP:

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="filename.csv"');

echo '"email","f_name"...

The browser will recognize the output as CSV and respond accordingly, most likely with a file download prompt.

The same goes for ASP.net but I'm not sure how it's done there.

Hubro
  • 56,214
  • 69
  • 228
  • 381
1
public ActionResult Index()
{
    Response.Clear();
    Response.Clear();
    Response.ContentType = "application/CSV";
    Response.AddHeader("content-disposition", "attachment; filename=\"filename.csv\"");
    String temp = "email,f_name,l_name,c_name,ids,abc1@test.com,CG,Wander,C1,abc2@Atest.COM,Virginia,Dale,A & D";
    Response.Write(temp);
    Response.End();
    return View();
}
Mark
  • 1,455
  • 3
  • 28
  • 51
  • Should I use the above code in the controller action? Sorry, I am very new to mvc. – dotNetNewbie Apr 25 '12 at 20:03
  • Yes, in the controller would work. Although the Response.Write would have to be better. I'll edit my response some more. – Mark Apr 25 '12 at 20:41
0

There are a few ways to handle this one and I'm not completely sure on your target audience, the environment, and your over-arching goals for this data.

I'll go with the kitchen sink route (you can use the data in the page AND allow it to be exported to file):

You can use jquery to wrap the data into a file for download.

This tutorial uses php on server side, but if I understand your requirements, you need to handle that data once it's on the client side:

http://tutorialzine.com/2011/05/generating-files-javascript-php/

See the section: assets/script.js

Excerpt:

$(document).ready(function(){

    $('#download').click(function(e){

        $.generateFile({
            filename    : 'export.txt',
            content     : $('textarea').val(),
            script      : 'download.php'
        });

        e.preventDefault();
    });

    $('#downloadPage').click(function(e){

        $.generateFile({
            filename    : 'page.html',
            content     : $('html').html(),
            script      : 'download.php'
        });

        e.preventDefault();
    });

});

Nice little example for dynamically generating a document.

JFish222
  • 1,026
  • 7
  • 11