Recommended way to create an ActionResult with a file extension
I need to create an ActionResult in an ASP.NET MVC application which has a .csv filetype.
I will provide a 'do not call' email list to my marketing partners and i want it to have a .csv extension in the filetype. Then it'll automatically open in Excel.
http://www.example.com/mailinglist/donotemaillist.csv?password=12334
I have successfully done this as follows, but I want to make sure this is the absolute best and recommended way of doing this.
[ActionName("DoNotEmailList.csv")]
public ContentResult DoNotEmailList(string username, string password)
return new ContentResult()
Content = Emails.Aggregate((a,b)=>a+Environment.NewLine + b),
ContentType = "text/csv"
;
This Actionmethod will respond to the above link just fine.
I'm just wondering if there is any likelihood of any unexpected conflict of having the file extension like this with any different version of IIS, any kind of ISAPI filter, or anything else I cant think of now.
I need to be 100% sure because I will be providing this to external partners and don't want to have to change my mind later. I really cant see any issues, but maybe theres something obscure - or another more "MVC" like way of doing this.
asp.net-mvc actionresult
add a comment |
I need to create an ActionResult in an ASP.NET MVC application which has a .csv filetype.
I will provide a 'do not call' email list to my marketing partners and i want it to have a .csv extension in the filetype. Then it'll automatically open in Excel.
http://www.example.com/mailinglist/donotemaillist.csv?password=12334
I have successfully done this as follows, but I want to make sure this is the absolute best and recommended way of doing this.
[ActionName("DoNotEmailList.csv")]
public ContentResult DoNotEmailList(string username, string password)
return new ContentResult()
Content = Emails.Aggregate((a,b)=>a+Environment.NewLine + b),
ContentType = "text/csv"
;
This Actionmethod will respond to the above link just fine.
I'm just wondering if there is any likelihood of any unexpected conflict of having the file extension like this with any different version of IIS, any kind of ISAPI filter, or anything else I cant think of now.
I need to be 100% sure because I will be providing this to external partners and don't want to have to change my mind later. I really cant see any issues, but maybe theres something obscure - or another more "MVC" like way of doing this.
asp.net-mvc actionresult
add a comment |
I need to create an ActionResult in an ASP.NET MVC application which has a .csv filetype.
I will provide a 'do not call' email list to my marketing partners and i want it to have a .csv extension in the filetype. Then it'll automatically open in Excel.
http://www.example.com/mailinglist/donotemaillist.csv?password=12334
I have successfully done this as follows, but I want to make sure this is the absolute best and recommended way of doing this.
[ActionName("DoNotEmailList.csv")]
public ContentResult DoNotEmailList(string username, string password)
return new ContentResult()
Content = Emails.Aggregate((a,b)=>a+Environment.NewLine + b),
ContentType = "text/csv"
;
This Actionmethod will respond to the above link just fine.
I'm just wondering if there is any likelihood of any unexpected conflict of having the file extension like this with any different version of IIS, any kind of ISAPI filter, or anything else I cant think of now.
I need to be 100% sure because I will be providing this to external partners and don't want to have to change my mind later. I really cant see any issues, but maybe theres something obscure - or another more "MVC" like way of doing this.
asp.net-mvc actionresult
I need to create an ActionResult in an ASP.NET MVC application which has a .csv filetype.
I will provide a 'do not call' email list to my marketing partners and i want it to have a .csv extension in the filetype. Then it'll automatically open in Excel.
http://www.example.com/mailinglist/donotemaillist.csv?password=12334
I have successfully done this as follows, but I want to make sure this is the absolute best and recommended way of doing this.
[ActionName("DoNotEmailList.csv")]
public ContentResult DoNotEmailList(string username, string password)
return new ContentResult()
Content = Emails.Aggregate((a,b)=>a+Environment.NewLine + b),
ContentType = "text/csv"
;
This Actionmethod will respond to the above link just fine.
I'm just wondering if there is any likelihood of any unexpected conflict of having the file extension like this with any different version of IIS, any kind of ISAPI filter, or anything else I cant think of now.
I need to be 100% sure because I will be providing this to external partners and don't want to have to change my mind later. I really cant see any issues, but maybe theres something obscure - or another more "MVC" like way of doing this.
asp.net-mvc actionresult
asp.net-mvc actionresult
asked Jun 13 '09 at 3:58
Simon_WeaverSimon_Weaver
72.6k61449521
72.6k61449521
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
I think your Response MUST contain "Content-Disposition" header in this case. Create custom ActionResult like this:
public class MyCsvResult : ActionResult
public string Content
get;
set;
public Encoding ContentEncoding
get;
set;
public string Name
get;
set;
public override void ExecuteResult(ControllerContext context)
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "text/csv";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
var fileName = "file.csv";
if(!String.IsNullOrEmpty(Name))
fileName = Name.Contains('.') ? Name : Name + ".csv";
response.AddHeader("Content-Disposition",
String.Format("attachment; filename=0", fileName));
if (Content != null)
response.Write(Content);
And use it in your Action instead of ContentResult:
return new MyCsvResult
Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
/* Optional
* , ContentEncoding = ""
* , Name = "DoNotEmailList.csv"
*/
;
thanks! but can you clarify what you mean by MUST ? what i had does appear to work - although this way is much more preferable
– Simon_Weaver
Jun 13 '09 at 17:41
If your Action returns ".csv" file then your clients want to "open" it or "save" it - so you must provide "Content-Disposition" header (although it is optional, see ietf.org/rfc/rfc2183.txt). Without this header the process of opening your file may differ in various browsers/OSes/machines
– eu-ge-ne
Jun 13 '09 at 18:06
1
Is there an error in the code? Shouldn't Name be a string instead of encoding? Also the compiler is telling me that ActionResult doesn't have a Response property
– ADB
Feb 7 '10 at 14:51
2
I've found the bug (Response.Headers.Add ...) - fixed (should beresponse.Headers.Add ...) - thanks again AD.
– eu-ge-ne
Feb 7 '10 at 16:42
5
Got an error when running this code (MVC2, ASP.Net 3.5): "This operation requires IIS integrated pipeline mode". Looks like need to change response.Headers.Add to response.AddHeader - platinumbay.com/blogs/dotneticated/archive/2010/10/07/…
– Andy Butland
Jan 12 '11 at 15:43
|
show 3 more comments
I used the FileContentResult action to also do something similar.
public FileContentResult DoNotEmailList(string username, string password)
string csv = Emails.Aggregate((a,b)=>a+Environment.NewLine + b);
byte csvBytes = ASCIIEncoding.ASCII.GetBytes( csv );
return File(csvBytes, "text/csv", "DoNotEmailList.csv");
It will add the content-disposition header for you.
1
Makes much more sense than writing a class to do something that's already built in! Cheers!
– Shawson
Apr 3 '12 at 14:39
This really is the way to go
– Joel Harris
Apr 3 '12 at 23:14
Thanks for a simple clean answer that is easy to implement in another situation
– Mattias Åslund
Mar 12 '13 at 6:57
add a comment |
This is how I'm doing something similar. I'm treating it as a download:
var disposition = String.Format(
"attachment;filename="0.csv"", this.Model.Name);
Response.AddHeader("content-disposition", disposition);
This should show up in the browser as a file download with the given filename.
I can't think of a reason why yours wouldn't work, though.
add a comment |
The answer you accepted is good enough, but it keeps the content of the output in memory as it outputs it. What if the file it generates is rather large? For example, when you dump a contents of the SQL table. Your application could run out of memory. What you do want in this case is to use FileStreamResult. One way to feed the data into the stream could be using pipe, as I described here
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f989927%2frecommended-way-to-create-an-actionresult-with-a-file-extension%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think your Response MUST contain "Content-Disposition" header in this case. Create custom ActionResult like this:
public class MyCsvResult : ActionResult
public string Content
get;
set;
public Encoding ContentEncoding
get;
set;
public string Name
get;
set;
public override void ExecuteResult(ControllerContext context)
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "text/csv";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
var fileName = "file.csv";
if(!String.IsNullOrEmpty(Name))
fileName = Name.Contains('.') ? Name : Name + ".csv";
response.AddHeader("Content-Disposition",
String.Format("attachment; filename=0", fileName));
if (Content != null)
response.Write(Content);
And use it in your Action instead of ContentResult:
return new MyCsvResult
Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
/* Optional
* , ContentEncoding = ""
* , Name = "DoNotEmailList.csv"
*/
;
thanks! but can you clarify what you mean by MUST ? what i had does appear to work - although this way is much more preferable
– Simon_Weaver
Jun 13 '09 at 17:41
If your Action returns ".csv" file then your clients want to "open" it or "save" it - so you must provide "Content-Disposition" header (although it is optional, see ietf.org/rfc/rfc2183.txt). Without this header the process of opening your file may differ in various browsers/OSes/machines
– eu-ge-ne
Jun 13 '09 at 18:06
1
Is there an error in the code? Shouldn't Name be a string instead of encoding? Also the compiler is telling me that ActionResult doesn't have a Response property
– ADB
Feb 7 '10 at 14:51
2
I've found the bug (Response.Headers.Add ...) - fixed (should beresponse.Headers.Add ...) - thanks again AD.
– eu-ge-ne
Feb 7 '10 at 16:42
5
Got an error when running this code (MVC2, ASP.Net 3.5): "This operation requires IIS integrated pipeline mode". Looks like need to change response.Headers.Add to response.AddHeader - platinumbay.com/blogs/dotneticated/archive/2010/10/07/…
– Andy Butland
Jan 12 '11 at 15:43
|
show 3 more comments
I think your Response MUST contain "Content-Disposition" header in this case. Create custom ActionResult like this:
public class MyCsvResult : ActionResult
public string Content
get;
set;
public Encoding ContentEncoding
get;
set;
public string Name
get;
set;
public override void ExecuteResult(ControllerContext context)
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "text/csv";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
var fileName = "file.csv";
if(!String.IsNullOrEmpty(Name))
fileName = Name.Contains('.') ? Name : Name + ".csv";
response.AddHeader("Content-Disposition",
String.Format("attachment; filename=0", fileName));
if (Content != null)
response.Write(Content);
And use it in your Action instead of ContentResult:
return new MyCsvResult
Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
/* Optional
* , ContentEncoding = ""
* , Name = "DoNotEmailList.csv"
*/
;
thanks! but can you clarify what you mean by MUST ? what i had does appear to work - although this way is much more preferable
– Simon_Weaver
Jun 13 '09 at 17:41
If your Action returns ".csv" file then your clients want to "open" it or "save" it - so you must provide "Content-Disposition" header (although it is optional, see ietf.org/rfc/rfc2183.txt). Without this header the process of opening your file may differ in various browsers/OSes/machines
– eu-ge-ne
Jun 13 '09 at 18:06
1
Is there an error in the code? Shouldn't Name be a string instead of encoding? Also the compiler is telling me that ActionResult doesn't have a Response property
– ADB
Feb 7 '10 at 14:51
2
I've found the bug (Response.Headers.Add ...) - fixed (should beresponse.Headers.Add ...) - thanks again AD.
– eu-ge-ne
Feb 7 '10 at 16:42
5
Got an error when running this code (MVC2, ASP.Net 3.5): "This operation requires IIS integrated pipeline mode". Looks like need to change response.Headers.Add to response.AddHeader - platinumbay.com/blogs/dotneticated/archive/2010/10/07/…
– Andy Butland
Jan 12 '11 at 15:43
|
show 3 more comments
I think your Response MUST contain "Content-Disposition" header in this case. Create custom ActionResult like this:
public class MyCsvResult : ActionResult
public string Content
get;
set;
public Encoding ContentEncoding
get;
set;
public string Name
get;
set;
public override void ExecuteResult(ControllerContext context)
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "text/csv";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
var fileName = "file.csv";
if(!String.IsNullOrEmpty(Name))
fileName = Name.Contains('.') ? Name : Name + ".csv";
response.AddHeader("Content-Disposition",
String.Format("attachment; filename=0", fileName));
if (Content != null)
response.Write(Content);
And use it in your Action instead of ContentResult:
return new MyCsvResult
Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
/* Optional
* , ContentEncoding = ""
* , Name = "DoNotEmailList.csv"
*/
;
I think your Response MUST contain "Content-Disposition" header in this case. Create custom ActionResult like this:
public class MyCsvResult : ActionResult
public string Content
get;
set;
public Encoding ContentEncoding
get;
set;
public string Name
get;
set;
public override void ExecuteResult(ControllerContext context)
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "text/csv";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
var fileName = "file.csv";
if(!String.IsNullOrEmpty(Name))
fileName = Name.Contains('.') ? Name : Name + ".csv";
response.AddHeader("Content-Disposition",
String.Format("attachment; filename=0", fileName));
if (Content != null)
response.Write(Content);
And use it in your Action instead of ContentResult:
return new MyCsvResult
Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
/* Optional
* , ContentEncoding = ""
* , Name = "DoNotEmailList.csv"
*/
;
edited Feb 2 '13 at 20:12
Dylan Beattie
38.9k26113175
38.9k26113175
answered Jun 13 '09 at 12:19
eu-ge-neeu-ge-ne
25.7k66562
25.7k66562
thanks! but can you clarify what you mean by MUST ? what i had does appear to work - although this way is much more preferable
– Simon_Weaver
Jun 13 '09 at 17:41
If your Action returns ".csv" file then your clients want to "open" it or "save" it - so you must provide "Content-Disposition" header (although it is optional, see ietf.org/rfc/rfc2183.txt). Without this header the process of opening your file may differ in various browsers/OSes/machines
– eu-ge-ne
Jun 13 '09 at 18:06
1
Is there an error in the code? Shouldn't Name be a string instead of encoding? Also the compiler is telling me that ActionResult doesn't have a Response property
– ADB
Feb 7 '10 at 14:51
2
I've found the bug (Response.Headers.Add ...) - fixed (should beresponse.Headers.Add ...) - thanks again AD.
– eu-ge-ne
Feb 7 '10 at 16:42
5
Got an error when running this code (MVC2, ASP.Net 3.5): "This operation requires IIS integrated pipeline mode". Looks like need to change response.Headers.Add to response.AddHeader - platinumbay.com/blogs/dotneticated/archive/2010/10/07/…
– Andy Butland
Jan 12 '11 at 15:43
|
show 3 more comments
thanks! but can you clarify what you mean by MUST ? what i had does appear to work - although this way is much more preferable
– Simon_Weaver
Jun 13 '09 at 17:41
If your Action returns ".csv" file then your clients want to "open" it or "save" it - so you must provide "Content-Disposition" header (although it is optional, see ietf.org/rfc/rfc2183.txt). Without this header the process of opening your file may differ in various browsers/OSes/machines
– eu-ge-ne
Jun 13 '09 at 18:06
1
Is there an error in the code? Shouldn't Name be a string instead of encoding? Also the compiler is telling me that ActionResult doesn't have a Response property
– ADB
Feb 7 '10 at 14:51
2
I've found the bug (Response.Headers.Add ...) - fixed (should beresponse.Headers.Add ...) - thanks again AD.
– eu-ge-ne
Feb 7 '10 at 16:42
5
Got an error when running this code (MVC2, ASP.Net 3.5): "This operation requires IIS integrated pipeline mode". Looks like need to change response.Headers.Add to response.AddHeader - platinumbay.com/blogs/dotneticated/archive/2010/10/07/…
– Andy Butland
Jan 12 '11 at 15:43
thanks! but can you clarify what you mean by MUST ? what i had does appear to work - although this way is much more preferable
– Simon_Weaver
Jun 13 '09 at 17:41
thanks! but can you clarify what you mean by MUST ? what i had does appear to work - although this way is much more preferable
– Simon_Weaver
Jun 13 '09 at 17:41
If your Action returns ".csv" file then your clients want to "open" it or "save" it - so you must provide "Content-Disposition" header (although it is optional, see ietf.org/rfc/rfc2183.txt). Without this header the process of opening your file may differ in various browsers/OSes/machines
– eu-ge-ne
Jun 13 '09 at 18:06
If your Action returns ".csv" file then your clients want to "open" it or "save" it - so you must provide "Content-Disposition" header (although it is optional, see ietf.org/rfc/rfc2183.txt). Without this header the process of opening your file may differ in various browsers/OSes/machines
– eu-ge-ne
Jun 13 '09 at 18:06
1
1
Is there an error in the code? Shouldn't Name be a string instead of encoding? Also the compiler is telling me that ActionResult doesn't have a Response property
– ADB
Feb 7 '10 at 14:51
Is there an error in the code? Shouldn't Name be a string instead of encoding? Also the compiler is telling me that ActionResult doesn't have a Response property
– ADB
Feb 7 '10 at 14:51
2
2
I've found the bug (
Response.Headers.Add ...) - fixed (should be response.Headers.Add ...) - thanks again AD.– eu-ge-ne
Feb 7 '10 at 16:42
I've found the bug (
Response.Headers.Add ...) - fixed (should be response.Headers.Add ...) - thanks again AD.– eu-ge-ne
Feb 7 '10 at 16:42
5
5
Got an error when running this code (MVC2, ASP.Net 3.5): "This operation requires IIS integrated pipeline mode". Looks like need to change response.Headers.Add to response.AddHeader - platinumbay.com/blogs/dotneticated/archive/2010/10/07/…
– Andy Butland
Jan 12 '11 at 15:43
Got an error when running this code (MVC2, ASP.Net 3.5): "This operation requires IIS integrated pipeline mode". Looks like need to change response.Headers.Add to response.AddHeader - platinumbay.com/blogs/dotneticated/archive/2010/10/07/…
– Andy Butland
Jan 12 '11 at 15:43
|
show 3 more comments
I used the FileContentResult action to also do something similar.
public FileContentResult DoNotEmailList(string username, string password)
string csv = Emails.Aggregate((a,b)=>a+Environment.NewLine + b);
byte csvBytes = ASCIIEncoding.ASCII.GetBytes( csv );
return File(csvBytes, "text/csv", "DoNotEmailList.csv");
It will add the content-disposition header for you.
1
Makes much more sense than writing a class to do something that's already built in! Cheers!
– Shawson
Apr 3 '12 at 14:39
This really is the way to go
– Joel Harris
Apr 3 '12 at 23:14
Thanks for a simple clean answer that is easy to implement in another situation
– Mattias Åslund
Mar 12 '13 at 6:57
add a comment |
I used the FileContentResult action to also do something similar.
public FileContentResult DoNotEmailList(string username, string password)
string csv = Emails.Aggregate((a,b)=>a+Environment.NewLine + b);
byte csvBytes = ASCIIEncoding.ASCII.GetBytes( csv );
return File(csvBytes, "text/csv", "DoNotEmailList.csv");
It will add the content-disposition header for you.
1
Makes much more sense than writing a class to do something that's already built in! Cheers!
– Shawson
Apr 3 '12 at 14:39
This really is the way to go
– Joel Harris
Apr 3 '12 at 23:14
Thanks for a simple clean answer that is easy to implement in another situation
– Mattias Åslund
Mar 12 '13 at 6:57
add a comment |
I used the FileContentResult action to also do something similar.
public FileContentResult DoNotEmailList(string username, string password)
string csv = Emails.Aggregate((a,b)=>a+Environment.NewLine + b);
byte csvBytes = ASCIIEncoding.ASCII.GetBytes( csv );
return File(csvBytes, "text/csv", "DoNotEmailList.csv");
It will add the content-disposition header for you.
I used the FileContentResult action to also do something similar.
public FileContentResult DoNotEmailList(string username, string password)
string csv = Emails.Aggregate((a,b)=>a+Environment.NewLine + b);
byte csvBytes = ASCIIEncoding.ASCII.GetBytes( csv );
return File(csvBytes, "text/csv", "DoNotEmailList.csv");
It will add the content-disposition header for you.
edited Nov 14 '18 at 13:00
A Boschman
2,08122739
2,08122739
answered Oct 19 '11 at 12:55
Peter RieszPeter Riesz
1,5391622
1,5391622
1
Makes much more sense than writing a class to do something that's already built in! Cheers!
– Shawson
Apr 3 '12 at 14:39
This really is the way to go
– Joel Harris
Apr 3 '12 at 23:14
Thanks for a simple clean answer that is easy to implement in another situation
– Mattias Åslund
Mar 12 '13 at 6:57
add a comment |
1
Makes much more sense than writing a class to do something that's already built in! Cheers!
– Shawson
Apr 3 '12 at 14:39
This really is the way to go
– Joel Harris
Apr 3 '12 at 23:14
Thanks for a simple clean answer that is easy to implement in another situation
– Mattias Åslund
Mar 12 '13 at 6:57
1
1
Makes much more sense than writing a class to do something that's already built in! Cheers!
– Shawson
Apr 3 '12 at 14:39
Makes much more sense than writing a class to do something that's already built in! Cheers!
– Shawson
Apr 3 '12 at 14:39
This really is the way to go
– Joel Harris
Apr 3 '12 at 23:14
This really is the way to go
– Joel Harris
Apr 3 '12 at 23:14
Thanks for a simple clean answer that is easy to implement in another situation
– Mattias Åslund
Mar 12 '13 at 6:57
Thanks for a simple clean answer that is easy to implement in another situation
– Mattias Åslund
Mar 12 '13 at 6:57
add a comment |
This is how I'm doing something similar. I'm treating it as a download:
var disposition = String.Format(
"attachment;filename="0.csv"", this.Model.Name);
Response.AddHeader("content-disposition", disposition);
This should show up in the browser as a file download with the given filename.
I can't think of a reason why yours wouldn't work, though.
add a comment |
This is how I'm doing something similar. I'm treating it as a download:
var disposition = String.Format(
"attachment;filename="0.csv"", this.Model.Name);
Response.AddHeader("content-disposition", disposition);
This should show up in the browser as a file download with the given filename.
I can't think of a reason why yours wouldn't work, though.
add a comment |
This is how I'm doing something similar. I'm treating it as a download:
var disposition = String.Format(
"attachment;filename="0.csv"", this.Model.Name);
Response.AddHeader("content-disposition", disposition);
This should show up in the browser as a file download with the given filename.
I can't think of a reason why yours wouldn't work, though.
This is how I'm doing something similar. I'm treating it as a download:
var disposition = String.Format(
"attachment;filename="0.csv"", this.Model.Name);
Response.AddHeader("content-disposition", disposition);
This should show up in the browser as a file download with the given filename.
I can't think of a reason why yours wouldn't work, though.
edited Jun 13 '09 at 4:09
answered Jun 13 '09 at 4:03
TalljoeTalljoe
12.4k23437
12.4k23437
add a comment |
add a comment |
The answer you accepted is good enough, but it keeps the content of the output in memory as it outputs it. What if the file it generates is rather large? For example, when you dump a contents of the SQL table. Your application could run out of memory. What you do want in this case is to use FileStreamResult. One way to feed the data into the stream could be using pipe, as I described here
add a comment |
The answer you accepted is good enough, but it keeps the content of the output in memory as it outputs it. What if the file it generates is rather large? For example, when you dump a contents of the SQL table. Your application could run out of memory. What you do want in this case is to use FileStreamResult. One way to feed the data into the stream could be using pipe, as I described here
add a comment |
The answer you accepted is good enough, but it keeps the content of the output in memory as it outputs it. What if the file it generates is rather large? For example, when you dump a contents of the SQL table. Your application could run out of memory. What you do want in this case is to use FileStreamResult. One way to feed the data into the stream could be using pipe, as I described here
The answer you accepted is good enough, but it keeps the content of the output in memory as it outputs it. What if the file it generates is rather large? For example, when you dump a contents of the SQL table. Your application could run out of memory. What you do want in this case is to use FileStreamResult. One way to feed the data into the stream could be using pipe, as I described here
edited May 23 '17 at 12:32
Community♦
11
11
answered Jun 5 '15 at 16:03
galetsgalets
8,741135991
8,741135991
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f989927%2frecommended-way-to-create-an-actionresult-with-a-file-extension%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown