CSV file being returned as a single string

NullDev

Here's the relevant code:

Controller:

public ActionResult APIDownloadCSV()
{
    var filename = Server.MapPath("~/xxx/test.csv");
    byte[] content = System.IO.File.ReadAllBytes(filename);
    return File(content, "text/csv", "test.csv");
}

Javascript:

var fetchCSV = function () {
    return postDownloadCsv().then(function (data) {
        var hiddenElement = document.createElement("a");
        hiddenElement.href = "data:text/csv;charset=utf-8," + data;
        hiddenElement.target = "_blank";
        hiddenElement.download = "test.csv";
        hiddenElement.click();
        hiddenElement.remove();
    });
}

postDownloadCSV is just a simple one-line $http.post function to the correct URL.

Problem:

When I use the above code, the file downloads as a CSV but returns only as a single continuous string. However, when I directly post the URL to the browser's location bar, the file being downloaded is correctly parsed. I've tried manipulating headers so that I'm requesting text/csv but it's not working. For reasons, I must not employ a parser on the client-side to fix this (Not that I'd want to, anyway. The file is perfectly alright on the server. Just need to download it.)

I suppose the problem to be in the side of the client as I can download the file through the URL, but where? Am I doing something wrong with my $http.post?

w33z33

try to encode the href of the link before you set it.

hiddenElement.href = encodeURI("data:text/csv;charset=utf-8," + data);

working js-fiddle: http://jsfiddle.net/f6enw0L6/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

String not being returned

From Dev

Why is a Flux<String> being collapsed into a single String when returned via ServerResponse, unless each String element is terminated with "\n"?

From Dev

Replace a string in a CSV file column with another string with a single quote

From Dev

Index file being returned for all requests

From Dev

Convert a single string formatted as a csv file into a pandas dataframe

From Dev

R - Read a SPECIFIC, Single NUMERIC column in CSV file as a string

From Dev

Parse a single CSV string?

From Dev

Unable to display multiple rows only a single row is being returned

From Dev

Delete file returned by "mpc search" with single command

From Dev

Value returned by DataBinder.Eval is being interpreted as a variable instead of a string

From Dev

std::stringstream.str() is always the same string being returned?

From Dev

evaluating a string of code in beanshell and getting the value being returned by the beanshell interpreter

From Dev

evaluating a string of code in beanshell and getting the value being returned by the beanshell interpreter

From Dev

Does EPOLLONESHOT prevent multiple events on a single descriptor from being returned in a single call to epoll_wait()?

From Dev

Transpose a single column of a CSV file

From Dev

Generating single hash of CSV file

From Dev

Reading from a CSV file while it is being written to

From Dev

CSV file content is being loaded with "quotes"

From Dev

Fields of rows in CSV file not being seperated

From Dev

Why is tuple being returned?

From Dev

Href values are not being returned

From Dev

Values not being returned

From Dev

values not being returned in dataframe

From Dev

Python dictionary being returned

From Dev

Rails file field being interpreted as String?

From Dev

String is not being directed to Output file (C++)

From Dev

All objects in array (interpreted from csv) being returned as the same object (the last object)

From Dev

Eclipse: File matching strategy being called on single click of any view

From Dev

Replace recurring string in a file with a single string

Related Related

  1. 1

    String not being returned

  2. 2

    Why is a Flux<String> being collapsed into a single String when returned via ServerResponse, unless each String element is terminated with "\n"?

  3. 3

    Replace a string in a CSV file column with another string with a single quote

  4. 4

    Index file being returned for all requests

  5. 5

    Convert a single string formatted as a csv file into a pandas dataframe

  6. 6

    R - Read a SPECIFIC, Single NUMERIC column in CSV file as a string

  7. 7

    Parse a single CSV string?

  8. 8

    Unable to display multiple rows only a single row is being returned

  9. 9

    Delete file returned by "mpc search" with single command

  10. 10

    Value returned by DataBinder.Eval is being interpreted as a variable instead of a string

  11. 11

    std::stringstream.str() is always the same string being returned?

  12. 12

    evaluating a string of code in beanshell and getting the value being returned by the beanshell interpreter

  13. 13

    evaluating a string of code in beanshell and getting the value being returned by the beanshell interpreter

  14. 14

    Does EPOLLONESHOT prevent multiple events on a single descriptor from being returned in a single call to epoll_wait()?

  15. 15

    Transpose a single column of a CSV file

  16. 16

    Generating single hash of CSV file

  17. 17

    Reading from a CSV file while it is being written to

  18. 18

    CSV file content is being loaded with "quotes"

  19. 19

    Fields of rows in CSV file not being seperated

  20. 20

    Why is tuple being returned?

  21. 21

    Href values are not being returned

  22. 22

    Values not being returned

  23. 23

    values not being returned in dataframe

  24. 24

    Python dictionary being returned

  25. 25

    Rails file field being interpreted as String?

  26. 26

    String is not being directed to Output file (C++)

  27. 27

    All objects in array (interpreted from csv) being returned as the same object (the last object)

  28. 28

    Eclipse: File matching strategy being called on single click of any view

  29. 29

    Replace recurring string in a file with a single string

HotTag

Archive