下载XLSX文件时收到$.ajax"parsererror"
Receive $.ajax "parsererror" when downloading XLSX file
下载 .xlsx 文件时,请求成功但我的 ajax 错误回调抛出一条 "parsererror - Invalid xml" 消息。
我的应用程序从服务器请求文件,该服务器是从另一台服务器获取该文件的代理。
在所有三台机器上,我在 .htaccess 中设置了以下内容:
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
ajax 调用的结构如下:
var attachment = { name="myfile.xlsx",
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx",
id="resource_id_on_remote-server"}
var path = '/path_to/attachment_file.php?' + $.param(attachment);
$.ajax({
type: "GET",
url: document.getElementById('attachment_download_frame').src = path,
success: function(xhrResponse) {
},
error: function(xhrResponse, errmsg, error) {
//errmsg = "parsererror"
},
complete: function() {
$('#spinner').spin("modal");
}
});
我通过实际发起请求的隐藏 Iframe 汇集调用。
服务器文件,attachment_file.php 使用 "attachment" json 参数从主机服务器请求附件文件:
$id = filter_input(INPUT_GET, 'id');
$contentType = filter_input(INPUT_GET, 'content_type');
$name = filter_input(INPUT_GET, 'name');
$url = REMOTE_BASE_URL . '/api/v1/attachment.json?';
$url .= http_build_query(array('id' => $id, 'name' => $name, 'content_type' => $contentType));
$curl = curl_init($url);
$headers = array(
'Content-Type: ' . $contentType
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPGET, true);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($response && in_array($httpCode, array(200, 201, 204))) {
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-Type: {$contentType}");
header("Content-Disposition: attachment; filename={$name}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen('php://output', 'w');
fputs($fh, $response);
} else {
}
请注意 "Content-Type" 是如何保存的。我已经验证这确实设置为:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx"
调用的 api 服务器是一个 Rails 应用程序,它使用传入的参数构建对文件资源的请求:
def fetch_attachment options
url = REMOTE_URL + '/my_attachments/' + options[:id]
response = HTTParty.get(url)
if status? response
return response.parsed_response
else
return false;
end
return false;
end
正在使用 HTTParty.get 作为请求。在这个刷脸的时刻,我会调查一下HTTParty请求中是否丢失了content type
此外,出于特定原因,我正在从参数中设置 mime 类型。
此 post 末尾的 GET 请求仅获取文件内容。它不获取文件。因此,发回的文件内容可能缺少 MS 文件的必要编码步骤,从而抛出(此时,完全合法的)解析器错误。
这充其量是一个不稳定的解决方案,但看起来端点可以就内容和 mime 类型达成一致,以将文件呈现给最终用户。
不幸的是,我们无法控制最终 GET 请求,只能以 BLOB 形式访问文件数据,并相信 mime 类型的编码是正确的。
下载 .xlsx 文件时,请求成功但我的 ajax 错误回调抛出一条 "parsererror - Invalid xml" 消息。 我的应用程序从服务器请求文件,该服务器是从另一台服务器获取该文件的代理。
在所有三台机器上,我在 .htaccess 中设置了以下内容:
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
ajax 调用的结构如下:
var attachment = { name="myfile.xlsx",
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx",
id="resource_id_on_remote-server"}
var path = '/path_to/attachment_file.php?' + $.param(attachment);
$.ajax({
type: "GET",
url: document.getElementById('attachment_download_frame').src = path,
success: function(xhrResponse) {
},
error: function(xhrResponse, errmsg, error) {
//errmsg = "parsererror"
},
complete: function() {
$('#spinner').spin("modal");
}
});
我通过实际发起请求的隐藏 Iframe 汇集调用。
服务器文件,attachment_file.php 使用 "attachment" json 参数从主机服务器请求附件文件:
$id = filter_input(INPUT_GET, 'id');
$contentType = filter_input(INPUT_GET, 'content_type');
$name = filter_input(INPUT_GET, 'name');
$url = REMOTE_BASE_URL . '/api/v1/attachment.json?';
$url .= http_build_query(array('id' => $id, 'name' => $name, 'content_type' => $contentType));
$curl = curl_init($url);
$headers = array(
'Content-Type: ' . $contentType
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPGET, true);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($response && in_array($httpCode, array(200, 201, 204))) {
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-Type: {$contentType}");
header("Content-Disposition: attachment; filename={$name}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen('php://output', 'w');
fputs($fh, $response);
} else {
}
请注意 "Content-Type" 是如何保存的。我已经验证这确实设置为:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx"
调用的 api 服务器是一个 Rails 应用程序,它使用传入的参数构建对文件资源的请求:
def fetch_attachment options
url = REMOTE_URL + '/my_attachments/' + options[:id]
response = HTTParty.get(url)
if status? response
return response.parsed_response
else
return false;
end
return false;
end
正在使用 HTTParty.get 作为请求。在这个刷脸的时刻,我会调查一下HTTParty请求中是否丢失了content type
此外,出于特定原因,我正在从参数中设置 mime 类型。
此 post 末尾的 GET 请求仅获取文件内容。它不获取文件。因此,发回的文件内容可能缺少 MS 文件的必要编码步骤,从而抛出(此时,完全合法的)解析器错误。
这充其量是一个不稳定的解决方案,但看起来端点可以就内容和 mime 类型达成一致,以将文件呈现给最终用户。
不幸的是,我们无法控制最终 GET 请求,只能以 BLOB 形式访问文件数据,并相信 mime 类型的编码是正确的。