写一段代码实现网页弹窗选择文件上传,并将文件保存至服务器指定位置
时间: 2023-03-13 11:29:07 浏览: 68
<script>
function uploadFile(){
var fileUpload = document.getElementById("fileUpload");
fileUpload.addEventListener("change", function () {
var file = fileUpload.files[0];
if (file) {
var formData = new FormData();
formData.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'upload.php');
xhr.send(formData);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
alert('上传成功!');
} else {
alert('上传失败!');
}
}
}
}
}, false);
}
</script>
相关问题
用vue3和elementplus写一段代码,要求能上传文件,并把文件名展示在弹窗中
好的,以下是符合您要求的代码示例:
```html
<template>
<div>
<el-button @click="dialogVisible = true">上传文件</el-button>
<el-dialog :visible.sync="dialogVisible" title="上传文件" width="30%">
<el-upload
class="upload-demo"
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
:on-success="handleSuccess"
:before-upload="beforeUpload"
:auto-upload="false"
>
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-progress
class="progress"
:stroke-width="4"
:percentage="uploadPercent"
:color="uploadPercent === 100 ? '#00bfff' : '#409eff'"
:status="uploadPercent === 100 ? 'success' : ''"
:text-inside="true"
/>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" :disabled="!uploadDone" @click="submitForm">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { ref } from 'vue'
import { ElUpload, ElButton, ElDialog, ElProgress } from 'element-plus'
export default {
components: { ElUpload, ElButton, ElDialog, ElProgress },
setup() {
const dialogVisible = ref(false)
const uploadDone = ref(false)
const uploadPercent = ref(0)
const fileName = ref('')
const beforeUpload = (file) => {
const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
const isLt500K = file.size / 1024 < 500
if (!isJPG) {
this.$message.error('上传文件只能是 JPG/PNG 格式!')
return false
}
if (!isLt500K) {
this.$message.error('上传文件大小不能超过 500KB!')
return false
}
return true
}
const handleSuccess = (response, file) => {
uploadDone.value = true
fileName.value = file.name
}
const submitForm = () => {
console.log('提交文件:', fileName.value)
dialogVisible.value = false
}
return {
dialogVisible,
uploadDone,
uploadPercent,
beforeUpload,
handleSuccess,
submitForm,
fileName
}
}
}
</script>
```
这段代码使用了Element Plus中的`el-upload`组件实现文件上传功能,上传成功后会将`uploadDone`和`fileName`的值进行更新。弹窗的展示使用了`el-dialog`组件,其中`visible`属性通过`v-model`与`dialogVisible`变量进行双向绑定。最后,当用户点击确定按钮时,会将文件名传递给后端进行处理。
写一段html代码 实现一个弹窗功能
### 回答1:
以下是一个基本的 HTML 弹窗功能实现代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML 弹窗功能实现</title>
<style>
.popup {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.popup-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<button onclick="showPopup()">显示弹窗</button>
<div id="popup" class="popup">
<div class="popup-content">
<span class="close" onclick="hidePopup()">×</span>
<p>这是一个弹窗。</p>
</div>
</div>
<script>
function showPopup() {
document.getElementById("popup").style.display = "block";
}
function hidePopup() {
document.getElementById("popup").style.display = "none";
}
</script>
</body>
</html>
```
此代码包含了基本的 HTML、CSS 和 JavaScript 代码。在 HTML 中,我们定义了一个按钮,它被点击时会触发 `showPopup()` 函数来显示弹窗。弹窗本身是一个 `div` 元素,它被定义为隐藏状态,并且包含一个 `popup-content` 元素,其中包含了弹窗的内容和关闭按钮。在 CSS 中,我们定义了 `popup` 和 `popup-content` 类,用于控制弹窗和其内部元素的样式。JavaScript 部分包含了 `showPopup()` 和 `hidePopup()` 函数,用于控制弹窗的显示和隐藏。
### 回答2:
<html>
<head>
<style>
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<h2>点击按钮弹出窗口</h2>
<button id="myBtn">打开弹窗</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>这是一个弹窗。</p>
</div>
</div>
<script>
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
### 回答3:
<!DOCTYPE html>
<html>
<head>
<style>
.overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
z-index: 1;
}
.popup {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
width: 300px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
z-index: 2;
}
.close-btn {
position: absolute;
top: 10px;
right: 10px;
width: 20px;
height: 20px;
cursor: pointer;
}
</style>
</head>
<body>
<button onclick="openPopup()">打开弹窗</button>
<div class="overlay" onclick="closePopup()"></div>
<div class="popup" id="popup">
<h1>弹窗内容</h1>
<p>这是一个弹窗的示例内容。</p>
<img src="close.png" class="close-btn" onclick="closePopup()" alt="关闭按钮">
</div>
<script>
function openPopup() {
var overlay = document.querySelector(".overlay");
var popup = document.querySelector("#popup");
overlay.style.display = "block";
popup.style.display = "block";
}
function closePopup() {
var overlay = document.querySelector(".overlay");
var popup = document.querySelector("#popup");
overlay.style.display = "none";
popup.style.display = "none";
}
</script>
</body>
</html>
以上是实现一个弹窗功能的HTML代码。当点击"打开弹窗"按钮时,通过JavaScript脚本控制弹窗的显示与隐藏,同时添加了遮罩层来阻止用户在弹窗显示时与背后的内容进行交互。弹窗中包括标题、内容和关闭按钮,用户可点击关闭按钮或弹窗外的遮罩层来关闭弹窗。