【发布时间】:2015-12-08 15:33:06
【问题描述】:
我发现了这个问题并关注了它downloading a file that comes as an attachment in a POST request response in PhantomJs。但是它保存的文件是一个html文件,上面写着404 - File or directory not found.我还有什么东西要下载这个csv文件吗?
这里是响应头
Cache-Control:private
Content-Disposition:attachment;filename=LenderCompetitionReport.csv
Content-Length:249487
Content-Type:application/csv
Date:Tue, 08 Dec 2015 14:32:47 GMT
Pragma:
Server:Microsoft-IIS/7.0
Set-Cookie:NSC_JOy4ob05etjhas0d2kv5wzcdlqr2zbu=ffffffff09149e4f45525d5f4f58455e445a4a423660;expires=Tue, 08-Dec-2015 14:34:47 GMT;path=/;secure;httponly
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
这是我的代码
casper.waitForSelector(x("//*[@id='ContentBody_ContentBody_btnExport']"),function() {
casper.then(function() {
var res = this.page.evaluate(function() {
var res={};
f=document.forms["frmBase"];
f.onsubmit= function() {
//iterate the form fields
var post={};
for(i=0; i<f.elements.length; i++) {
post[f.elements[i].name]=f.elements[i].value;
}
res.action = f.action;
res.post = post;
return false; //Stop form submission
}
//Trigger the click on the link.
var l = $("#ContentBody_BodyLevelElment_lbtnCsv");
l.click();
return res; //Return the form data to casper
});
//Start the download
casper.download(res.action, "LenderCompetitionReport.csv", "POST", res.post);
});
});
【问题讨论】:
-
明确一点:res.action 中有什么? casper.download() 需要一个 URL 作为它的第一个参数。
标签: javascript casperjs