저는 phpWord를 사용하여 XMLHttpRequest를 사용하여 호출되는 PHP 스크립트에서 즉시 워드 문서를 만듭니다. 요청에 대한 응답을 트래핑 한 다음 사용자에게 파일을 다운로드하거나 열 것인지 묻는 중입니다. 내 phpWord 코드는 파일 (예 : 서버에서 파일을 열 수 있음)을 만들고 브라우저에서 파일을 열거 나 저장하라는 메시지를 표시하지만 다운로드 한 파일은 어떻게 든 손상됩니다. "죄송합니다. 내용에 문제가있어서 결과를 열 수 없습니다."와 같은 오류가 나타납니다. 사람이 내가 잘못 갈거야 어디에서 발견 할 수있는 경우phpWord - javascript 클라이언트 측 문제를 사용하여 docx를 다운로드 할 때 파일 손상이 발생했습니다.
function getDOC()
{
url='services/doc_search_results_service.php/';
var req = null;
var postParms = '';
req = new XMLHttpRequest
if (req)
{
req.open('POST', url, true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", postParms.length);
req.setRequestHeader("Connection", "close");
req.onreadystatechange = function()
{
if (req.readyState == 4 && req.status == 200)
{
downloadDOC("results.docx", req.responseText);
}
}
req.send(postParms);
}
}
function downloadDOC(filename, text)
{
var pom = document.createElement('a');
pom.setAttribute('href', 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);
}
알고 싶어요 :이 코드가
...
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
flush();
readfile($filename);
클라이언트 측 : 서버 측에
이 코드를 . 나는 PHP 나 자바 스크립트 전문가가 아니므로 도움을 주시면 감사하겠습니다. FWIW 나는 헤더와 관련이 있다고 생각합니다. 나는 또한 파일이 서버 측에서 OK를 만들 때 클라이언트 측 문제가 될 수 있다고 생각한다.미리 감사드립니다. :-)
두 파일 (서버에서 다운로드 한 파일)을 비교해보십시오. 아마도 무언가 (예 : 잘린 내용, dunno)가 보일 것입니다. – MightyPork
정말 POST 요청이 필요합니까? 이것이 GET 요청이라면 ajax (현대 브라우저에서 가능)로 가능하지만, 'window.location = url;'을 수행하면됩니다. – Musa
musa와 mightypork는 귀하의 제안에 감사드립니다. 지금은 늦었습니다. (이미 너무 많은 시간을 들여 화면을 보았습니다.) 내일 첫 시도를 시도 할 것입니다. 감사합니다 크리스 –