js 파일 판독기와 AJAX를 사용하여 파일을 내 서버에 업로드하려고합니다.js fileReader를 사용하여 파일을 업로드하면 파일이 손상됩니다.
FileAPI과 fileReader을 사용하여 파일을 읽고 문자열로 변환 한 다음 AJAX 요청을 통해 서버로 보냈습니다.
다음function upload() {
var file = document.getElementById('periodExcel').files[0];
if (file) {
console.log(file);
var reader = new FileReader();
reader.readAsText(file, file.type);
console.log(reader);
reader.onload = uploadAndRun;
}
}
function uploadAndRun(event) {
console.log(event);
var result = event.target.result;
$('#js').html(result);
var fileName = document.getElementById('periodExcel').files[0].name; //Should be 'picture.jpg'
$.post('./upload.php', {data: result, name: fileName}, function(result2){
$('#php').html(result2);
});
}
가 업로드 PHP 스크립트입니다 :
file_put_contents('upload/' . $_POST['name'], $_POST['data']);
그냥 PHP를 file_put_contents
기능을 사용하여 파일을 작성 은 여기 내 클라이언트 측 JS 코드입니다.
내 문제는 업로드 된 파일이 손상되어 원본 파일 (크기가 더 크다)과 크기가 다른 것입니다.
동일한 파일을 읽고 file_put_contents
을 사용하여 다시 쓰는 함수 인 PHP file_get_contents
을 사용해 보았는데 결과 파일이 원래 파일과 동일하고 똑같습니다.
strcmp
를 사용하여 두 개의 문자열을 비교
fileReader
은
file_get_contents
에서 나온 것보다 큽니다.
내 코드의 문제점은 무엇이며 readAsText
기능을 사용하는 동안 FileReader를 사용하여 파일을 이런 식으로 업로드하는 방법은 무엇입니까?
파일을 업로드 할 때'enctype'이 올바르게 설정되도록하려면 ['FormData'] (https://developer.mozilla.org/en-US/docs/Web/API/FormData) API를 사용해야합니다. – apokryfos
I 그것을 사용하려했지만 채워지지 않고 (작동하지 않는 추가), 제출하지 못하게하는 오류가 발생합니다. –