0

I have a delimited string coming from a service, I want to convert this delimited string with headers to a downlodable CSV file.

    empId,empName,salary,depName,depId
    1,John,20000,IT,2

Could someone help me with a code snippet/reference.

1
  • Isn't this a CSV already? Do you just want to force a download? Commented Jul 13, 2015 at 5:18

1 Answer 1

1

change header first

<?php
    header('Content-Disposition: attachment; filename="downloaded.csv"');
    echo 'empId,empName,salary,depName,depId';
    echo '1,John,20000,IT,2';   
?>

and then echo your output.

Sign up to request clarification or add additional context in comments.

5 Comments

Sry, the service returns a CSV, the problem is the response goes to jQuery failCallback, I can see the CSV string in jqXHR.
set this also in header("Set-Cookie: fileDownload=true; path=/") and try
no, it doesnt help, i think something should be added to jquery get parameters, trying with several options like dataType: "text/csv", still facing problem
does it coming into sucess callback ?
i suggest to use iframe or window.location not to use jquery ajax. it will definitely work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.