php - XMLHttpRequest() and outputting csv file - Stack Overflow

I can successfully generate a csv file by issuing an html form POST to a new window and use PHP to resp

I can successfully generate a csv file by issuing an html form POST to a new window and use PHP to respond with:

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="'.date("Ymdhis").'.csv"');
print get_lines();

However, this leaves the the window open and blank.

I want to either close the window automatically (on recognition of the Content-Disposition) or I would prefer to ignore the window pletely and simply onClick, call XMLHttpRequest() send the form variables to the PHP, generate the data file and then prompt the user to save or open.

I have tried:

var xhReq = new XMLHttpRequest();
var parameters = "";

for ( i=0; i<formObj.elements.length; i++ ) {
  parameters += formObj.elements[i].name + "=" + encodeURI( formObj.elements[i].value ) + "&";
}

xhReq.open("POST", outputLocation, false);
xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhReq.setRequestHeader("Content-length", parameters.length);
xhReq.setRequestHeader("Connection", "close");
xhReq.send(parameters);

document.write(xhReq.responseText);

but this doesn't work because the response is simply printed as text to screen.

What I really want, is once I have all the data, to issue a new header() call without opening a new window - is this possible using javascript?

I can successfully generate a csv file by issuing an html form POST to a new window and use PHP to respond with:

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="'.date("Ymdhis").'.csv"');
print get_lines();

However, this leaves the the window open and blank.

I want to either close the window automatically (on recognition of the Content-Disposition) or I would prefer to ignore the window pletely and simply onClick, call XMLHttpRequest() send the form variables to the PHP, generate the data file and then prompt the user to save or open.

I have tried:

var xhReq = new XMLHttpRequest();
var parameters = "";

for ( i=0; i<formObj.elements.length; i++ ) {
  parameters += formObj.elements[i].name + "=" + encodeURI( formObj.elements[i].value ) + "&";
}

xhReq.open("POST", outputLocation, false);
xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhReq.setRequestHeader("Content-length", parameters.length);
xhReq.setRequestHeader("Connection", "close");
xhReq.send(parameters);

document.write(xhReq.responseText);

but this doesn't work because the response is simply printed as text to screen.

What I really want, is once I have all the data, to issue a new header() call without opening a new window - is this possible using javascript?

Share Improve this question edited Apr 30, 2010 at 6:51 sjw asked Apr 30, 2010 at 3:49 sjwsjw 2,6335 gold badges22 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You need to send the POST request in a new window in order to do this. I would suggest that you create a hidden form and set it's target attribute to _blank

You should set the Content-disposition HTTP header on the server-side to force a download.

You are calling document.write, which dumps a text string into the browser, this is likely not what you wish to do.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744853837a4597287.html

相关推荐

  • php - XMLHttpRequest() and outputting csv file - Stack Overflow

    I can successfully generate a csv file by issuing an html form POST to a new window and use PHP to resp

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信