【发布时间】:2015-11-20 11:53:23
【问题描述】:
因此,我使用 Google DFA Reporting API 来提取广告系列报告文件列表并下载最新的文件。
function transferData() {
var getFile = gapi.client.dfareporting.reports.files.get({
'profileId': '12345', // My DFA profile ID
'reportId': reportId, // The ID of the report type
'fileId': runId // The ID of the final report (corresponding to the report type defined earlier)
});
getFile.then(function (response) {
var URL = response.result.urls.browserUrl;
window.open(URL); // Open the download link
}, function (reason) {
console.log('Error: ' + reason.result.error.message);
});
}
这段代码返回 URL 变量,它本质上是 CSV 报告文件的下载链接,例如https://storage.googleapis.com/dfa_-700df587fcd884268484ea3d1/campaign_report.csv?GoogleAccessId=12345m@developer.gserviceaccount.com&Expires=12345&Signature=12345
但是现在我需要以某种方式解析这个 CSV 文件。我以前做的是立即让用户重新上传他们刚刚下载的 CSV 文件,这样我就可以通过 PHP 在服务器端解析它。但是,我想避免下载然后上传的过程,并稍微自动化一下。我尝试使用 AJAX,但偶然发现了一个 CORS 错误,指出跨源请求被阻止。使用 jsonp 作为数据类型绕过了 CORS 错误,但不能使用,因为服务器返回了一个 CSV 文件,对吧?
那么我是否可以通过 API 提供给我的下载 URL 自动解析 CSV 文件,而不需要用户在下载后立即重新上传 CSV?
【问题讨论】:
-
你为什么要让用户下载并重新上传文件 - 你当然应该将文件的链接发送到服务器并让服务器下载文件?
-
如果我想写一个插件所以没有服务器
标签: jquery ajax csv google-api cors