Nếu bạn muốn lưu trữ tệp hình ảnh và hiển thị bản xem trước mà không cần tải lại toàn bộ trang thì bạn cần sử dụng jQuery AJAX
Gửi tệp đã chọn bằng đối tượng FormData trong yêu cầu AJAX
nội dung1. HTML
Tạo phần tử đã thêm
Nếu bạn muốn lưu trữ tệp hình ảnh và hiển thị bản xem trước mà không cần tải lại toàn bộ trang thì bạn cần sử dụng jQuery AJAX
Gửi tệp đã chọn bằng đối tượng FormData trong yêu cầu AJAX
nội dung1. HTML
Tạo phần tử đã thêm
2. CSS
Ẩn phần tử
/* Container */0
.container{
margin: 0 auto;
border: 0px solid black;
width: 50%;
height: 250px;
border-radius: 3px;
background-color: ghostwhite;
text-align: center;
}
/* Preview */
.preview{
width: 100px;
height: 100px;
border: 1px solid black;
margin: 0 auto;
background: white;
}.preview img{
display: none;
}
/* Button */
.button{
border: 0px;
background-color: deepskyblue;
color: white;
padding: 5px 15px;
margin-left: 10px;
}
/* Container */
.container{
margin: 0 auto;
border: 0px solid black;
width: 50%;
height: 250px;
border-radius: 3px;
background-color: ghostwhite;
text-align: center;
}
/* Preview */
.preview{
width: 100px;
height: 100px;
border: 1px solid black;
margin: 0 auto;
background: white;
}.preview img{
display: none;
}
/* Button */
.button{
border: 0px;
background-color: deepskyblue;
color: white;
padding: 5px 15px;
margin-left: 10px;
}
3. PHP
Tạo tệp
/* Container */1 và thư mục
.container{
margin: 0 auto;
border: 0px solid black;
width: 50%;
height: 250px;
border-radius: 3px;
background-color: ghostwhite;
text-align: center;
}
/* Preview */
.preview{
width: 100px;
height: 100px;
border: 1px solid black;
margin: 0 auto;
background: white;
}.preview img{
display: none;
}
/* Button */
.button{
border: 0px;
background-color: deepskyblue;
color: white;
padding: 5px 15px;
margin-left: 10px;
}
/* Container */2 để lưu trữ tệp hình ảnh
.container{
margin: 0 auto;
border: 0px solid black;
width: 50%;
height: 250px;
border-radius: 3px;
background-color: ghostwhite;
text-align: center;
}
/* Preview */
.preview{
width: 100px;
height: 100px;
border: 1px solid black;
margin: 0 auto;
background: white;
}.preview img{
display: none;
}
/* Button */
.button{
border: 0px;
background-color: deepskyblue;
color: white;
padding: 5px 15px;
margin-left: 10px;
}
Kiểm tra phần mở rộng của tệp nếu nó hợp lệ thì lưu trữ tệp và trả lại vị trí tệp
Mã hoàn thành
Chủ Đề