locked
Download file using asp.net razor web pages. RRS feed

  • Question

  • User-1679949502 posted

    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. :(

    Monday, August 31, 2020 9:32 PM

Answers

  • User-1330468790 posted

    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.

    • 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 CsvHelper to 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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 1, 2020 3:03 AM