-1

I am making a dynamic CVS report file in my controller in the format of StringBuilder. I have tried this code to return the file:

public async Task GetCvsAsync(....) { .... //making the file

        using (MemoryStream stream = new MemoryStream())
        {
            StreamWriter objstreamwriter = new StreamWriter(stream);
            objstreamwriter.Write(res.ToString());
            objstreamwriter.Flush();
            objstreamwriter.Close();
            return File(stream.ToArray(), "application/octet-stream", "test.csv");
        }

    } 

it makes the correct result but in the console response header (nothing happens in the front) like this: enter image description here

if I right-click on the console and click open in a new tab, it will start to download the file. I have tried this way as well:

var f = File(Encoding.UTF8.GetBytes(res.ToString()), "text/csv", "authors.csv");
return f;

res is in the type of StringBuilder.

I want to download automatically begins after the file is ready, I have no idea what should I do.

note: this action called by a href tag onClick event on my razor page:

function cvs(meID, oID, tID) {
        $.get("/Dashboard/GetCVS?meID=" + meID
            + "&oID=" + oID+ "&tID=" + tID);
    }
Paria Shiri
  • 87
  • 1
  • 1
  • 9
  • 1
    It's not clear to me specifically what behavior you're describing. When you right click on the link and open in a new tab, it downloads the file as expected? But when you click on the link normally it doesn't? What does it do instead? – David Jun 21 '21 at 13:56
  • I didn't say that I right-click on the link, I said when I right-click on the console response (in the picture). in fact, nothing happening in the front (just if I check the console I can see this result ). is it clear now? – Paria Shiri Jun 21 '21 at 14:14
  • 1
    Ah, it looks like the issue is that you're trying to download a file via AJAX, but the client-side code isn't doing anything with the response. Does this answer your question? [Download File Using Javascript/jQuery](https://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery) – David Jun 21 '21 at 14:22

1 Answers1

-1

I couldn't solve my problem with jquery I just change my code to direct calling my action instead of ajax call and my problem solved!

Paria Shiri
  • 87
  • 1
  • 1
  • 9