웹 라벨링 도구를 사용하여 웹 사이트에서 이미지를 생성하고 있습니다. 그러나 라벨 지정을 마쳤을 때 이미지를 로컬로 다운로드하는 대신, 서버에 업로드하고 싶습니다. 이 함수는 자바 스크립트 파일에 있으며 JS와 관련된 모든 업로드는 양식 제출과 관련이 있습니다. 양식 및 슈퍼 전역 변수가 없습니다. 웹에 생성 된 이미지를 서버에 업로드하려면 어떻게해야합니까?require.js 모듈에 웹 생성 이미지 업로드하기
여기에 제가 현재 가지고있는 코드입니다. HTML 파일이 아니며, js 파일에 있습니다.
var file_data = annotator.export();
var formData = dataURItoBlob(file_data);
var fd = new FormData(document);
fd.append("resultImage",formData);
url="upload/";
http.open("POST", url, true);
// headers
http.setRequestHeader("Content-type", "application/x-www-form- urlencoded");
http.setRequestHeader("Content-length", fd.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(fd);
PHP 파일은
<?php
$upload_dir = "upload/";
$img = $_POST['hidden_data'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = $upload_dir . mktime() . ".png";
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
정말 감사합니다, 당신의 도움을 주셔서 감사합니다.
감사합니다. 에반스, 미안하지만 아직 upvote에 대한 충분한 명성이 없습니다. –