Question: I am using MVC 4.

I have a method in my controller which generates a CSV file on demand and I want this file to be then downloaded by the user without the need of saving it to the disk at the server side. So I am passing a MemoryStream on the File() object to avoid having to firstly save file to disk and later give him the path for download.

Controller Method:

```
[HttpGet]
public ActionResult GenerateCsv(string data)

    var sb = new StringBuilder();
    sb = GetCsvReportToString(data);

    var stream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));

    return this.File(stream, "text/csv", "ReportCsv.csv");
}

```

sb (StringBuilder) has the correct data, as I have debugged it and confirmed it.

my javascript code:

```
window.open(generateFileLink + '?data=' + dataToSend, '_blank');

```

The window gets in fact opened, the download dialog appears but with an error, saying that it cannot be downloaded from localhost:

![enter image description here](https://i.stack.imgur.com/GQfWh.png)

The name is different in the download "ReportToCsv" because I didn't use real names for methods in my question, but doesn't matter.

Can anyone assist?

Answer: 
The criteria for your `for` loop in `palindrome` doesn't look right:

```
for(unsigned int i = x_str.length(); i > 0; i++){
    x_str_rev += x_str[i];
};

```

I expect that you meant to go through the string backwards right? What you're doing in the above code snippet is starting at the end of the string and going off into no-man's-land, which is going to cause undefined behaviour.

You should use a decrementing loop like this:

```
for(unsigned int i = x_str.length() - 1; i >= 0; --i){
    x_str_rev += x_str[i];
};

```
Change

```
for (unsigned int i = x_str.length(); i > 0; i++){

```

to

```
for (unsigned int i = x_str.length()-1; i >= 0; --i){
                      ^^^^^^^^^^^^^^^^    ^^    ^^^

```