는 HTML 입력 제어
<input type="file" id="myFile" multiple size="50" onchange="myFunction()">
자바 스크립트 함수
function myFunction(){
var x = document.getElementById("myFile");
var txt = "";
var src="";
if ('files' in x) {
if (x.files.length == 0) {
txt = "Select one or more files.";
} else {
for (var i = 0; i < x.files.length; i++) {
txt += "<br><strong>" + (i+1) + ". file</strong><br>";
var file = x.files[i];
var data = new FormData();
data.append('picture', file);
jQuery.ajax({
url: '/upload',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
if ('name' in file) {
txt += "name: " + file.name + "<br>";
}
if ('size' in file) {
txt += "size: " + file.size + " bytes <br>";
}
}
}
}
else {
if (x.value == "") {
txt += "Select one or more files.";
} else {
txt += "The files property is not supported by your browser!";
txt += "<br>The path of the selected file: " + x.value; // If the browser does not support the files property, it will return the path of the selected file instead.
}
}
CONSOLE.LOG (TXT); }
경로
POST /upload controllers.Application.upload
그래서 당신은 메모리 데이터베이스에 업로드 된 이미지를 저장하는 컨트롤러
public static Result upload() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = body.getFile("picture");
if (picture != null) {
String fileName = picture.getFilename();
String contentType = picture.getContentType();
File file = picture.getFile();
return ok("File uploaded");
} else {
flash("error", "Missing file");
return redirect(routes.Application.index());
}
}
. 권리? DB 대신 파일 시스템에 이미지를 저장하고 파일 시스템에서 이미지를 검색하려면 어떻게 수정해야합니까? – Shivansh
바이트 배열 (Image-class)로 파일을 쓰는 대신 다음과 같이 말할 수 있습니다 : image.renameTo (new File ("public/images /", name)); 그것을 파일 시스템에 저장합니다. – DirtyDan
그리고 예를 들어 http : // localhost : 9000/assets/images/을 통해 다시 가져옵니다. –
DirtyDan