And it's working but this creates the headers in the 3rd row, leaving the 2 first rows empty. Then when the user upload the file I'm starting reading from the 4th row,,, and I was wondering if there is a way to create the headers in the first row.
Or a way to download a file(withoud creating it) using razor syntax. :(
Your problem is related to return a CSV file with headers to browser side. I can see that you want to use "Response" to achieve that.
There are two options that you could adopt to implement this function.
Modify your original html(cshtml) codes:
The reason why your codes does not work is that there are some "NewLines" in your page which will be returned and treated as lines.
@{
Layout = null;
WebSecurity.RequireAuthenticatedUser();
Response.Clear();
var filename = String.Format("createorder.csv");
Response.AddHeader("Content-disposition", "attachment; filename=" + filename);
Response.ContentType = "application/octet-stream";
int i = 0;
}
<!-- Here are some lines which will be written in the file -->
@if (i == 0)
{
@:"@Html.Raw("User Id")","@Html.Raw("Name")","@Html.Raw("Address Type")","@Html.Raw("Address")","@Html.Raw("Address 2")","@Html.Raw("City")","@Html.Raw("State")","@Html.Raw("Zip")", "@Html.Raw("Country")", "@Html.Raw("Phone")","@Html.Raw("Item Id")","@Html.Raw("Item Quantity")"
}
What you should do is to remove those blanks.
Use CsvHelperto write string stream to the response
You could refer to below codes:
private void DownLoadFile()
{
Response.Clear();
var filename = String.Format("createorder.csv");
Response.AddHeader("Content-disposition", "attachment; filename=" + filename);
Response.ContentType = "application/octet-stream";
// init StringWriter, StringBuilder
var sb = new StringBuilder();
List<Example> rptLines = new List<Example>();
using (var sw = new StringWriter(sb))
{
// init CsvWriter
var csv = new CsvWriter(sw, CultureInfo.InvariantCulture);
// write all rptLines records to my StringBuilder
csv.WriteRecords(rptLines);
}
Response.Write(sb.ToString());
Response.End();
}
public class Example
{
public string Id { set; get; }
public string Name { set; get; }
}
Result file will include headers which come from the member name of the class "Example".
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
1 Post
Download file using asp.net razor web pages.
Aug 31, 2020 09:32 PM|Lesslie|LINK
Hello, I have a problem. I'm working in this really old project from 2013... and I'm going crazy with this.
I don't have controllers or models and I'm trying to create an .CSV file so the user can use it as a template, then upload it!
I'm using this to create the template:
@{
Layout = null;
WebSecurity.RequireAuthenticatedUser();
Response.Clear();
var filename = String.Format("createorder.csv");
Response.AddHeader("Content-disposition", "attachment; filename=" + filename);
Response.ContentType = "application/octet-stream";
int i = 0;
}
@if (i == 0)
{
@:"@Html.Raw("User Id")","@Html.Raw("Name")","@Html.Raw("Address Type")","@Html.Raw("Address")","@Html.Raw("Address 2")","@Html.Raw("City")","@Html.Raw("State")","@Html.Raw("Zip")", "@Html.Raw("Country")", "@Html.Raw("Phone")","@Html.Raw("Item Id")","@Html.Raw("Item Quantity")"
}
And it's working but this creates the headers in the 3rd row, leaving the 2 first rows empty. Then when the user upload the file I'm starting reading from the 4th row,,, and I was wondering if there is a way to create the headers in the first row.
Or a way to download a file(withoud creating it) using razor syntax. :(
Contributor
2900 Points
852 Posts
Re: Download file using asp.net razor web pages.
Sep 01, 2020 03:03 AM|Sean Fang|LINK
Hi Lesslie,
Your problem is related to return a CSV file with headers to browser side. I can see that you want to use "Response" to achieve that.
There are two options that you could adopt to implement this function.
The reason why your codes does not work is that there are some "NewLines" in your page which will be returned and treated as lines.
@{ Layout = null; WebSecurity.RequireAuthenticatedUser(); Response.Clear(); var filename = String.Format("createorder.csv"); Response.AddHeader("Content-disposition", "attachment; filename=" + filename); Response.ContentType = "application/octet-stream"; int i = 0; } <!-- Here are some lines which will be written in the file --> @if (i == 0) { @:"@Html.Raw("User Id")","@Html.Raw("Name")","@Html.Raw("Address Type")","@Html.Raw("Address")","@Html.Raw("Address 2")","@Html.Raw("City")","@Html.Raw("State")","@Html.Raw("Zip")", "@Html.Raw("Country")", "@Html.Raw("Phone")","@Html.Raw("Item Id")","@Html.Raw("Item Quantity")" }
What you should do is to remove those blanks.
You could refer to below codes:
Result file will include headers which come from the member name of the class "Example".
Best regards,
Sean