html 文件上传区

w8ntj3qf  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(84)

你好,我有一个问题,我的代码上传文件在网站上.问题是,我不能得到的文件.我的意思是,我不能上传文件,因为该地区必须是工程它不工作.这是从视频教程.有人可以看看它,说我在哪里我有一个问题?非常感谢.

window.addEventListener("upload", ()=>{
  const input = document.getElementById("upload");
  const filewrapper = document.getElementById("filewrapper");

  input.addEventListener("change",(e)=>{
    let fileName = e.target.files[0].name;
    let filetype = e.target.value.split(".").pop();
    fileshow(fileName, filetype);
  })

  const fileshow=(fileName, filetype)=>{
    const showfileboxElem = document.createElement("div");
    showfileboxElem.classList.add("showfilebox");
    const leftElem = document.createElement("div");
    leftElem.classList.add("left");
    const fileTypeElem = document.createElement("span");
    fileTypeElem.classList.add("filetype")
    fileTypeElem.innerHTML=filetype;
    leftElem.append(fileTypeElem);
    const filetitleElem = document.createElement("h3");
    filetitleElem.innerHTML=fileName;
    leftElem.append(filetitleElem);
    showfileboxElem.append(leftElem);
    const rightElem = document.createElement("div");
    rightElem.classList.add("right");
    showfileboxElem.append(rightElem);
    const crossElem = document.createElement("span");
    crossElem.innerHTML="×";
    rightElem.append(crossElem);
    filewrapper.append(showfileboxElem);

    crossElem.addEventListener("click", ()=>{
      filewrapper.removeChild(showfileboxElem);
    })
  }
})
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: sans-serif;
}

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 15px;
  width: 100%;
  min-height: 100vh;
  background-color: rebeccapurple;
}

.box {
  max-width: 500px;
  background: #fff;
  padding: 30px;
  width: 100%;
  border-radius: 5px;
}

.upload-area-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 20px;
  font-weight: 600;
}

.uploadlabel {
  width: 100%;
  min-height: 100px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 3px dashed aqua;
  cursor: pointer;
}

.uploadlabel span {
  font-size: 70px;
  color: aqua;
}

.uploadlabel p {
  color: aqua;
  font-size: 20px;
  font-weight: 800;
  font-family: cursive;
}

.uploaded {
  margin: 30px 0;
  font-size: 16px;
  font-weight: 700;
  color: #222;
}

.showfilebox {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 10px 0;
  padding: 10px 15px;
  box-shadow: #00000d 0px 0px 0px 1px, #d1d5db3d 0px 0px 0px 1px inset;
}

.showfilebox .left {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.filetype {
  background: aqua;
  color: #fff;
  padding: 5px 15px;
  font-size: 20px;
  text-transform: capitalize;
  font-weight: 700;
  border-radius: 3px;
}

.left h3 {
  font-weight: 600;
  font-size: 18px;
  color: yellowgreen;
  margin: 0;
}

.right span {
  background: aqua;
  color: #fff;
  width: 25px;
  height: 25px;
  font-size: 25px;
  line-height: 25px;
  display: inline-block;
  text-align: center;
  font-weight: 700;
  cursor: pointer;
  border-radius: 50%;
}
<!DOCTYPE html>
<html lang="cs">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="description" content="" />
  <meta name="keywords" content="" />
  <meta name="author" content="Michal Kozumplík" />
  <link rel="stylesheet" href="" />
  <link rel="stylesheet" href="" />
  <link rel="stylesheet" href="files/style.css" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://kit.fontawesome.com/d8714c9903.js" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.2/jquery.min.js"
    integrity="sha512-tWHlutFnuG0C6nQRlpvrEhE4QpkG1nn2MOUMWmUeRePl4e3Aki0VB6W1v3oLjFtd0hVOtRQ9PHpSfN6u6/QXkQ=="
    crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  <!--[if lt IE 9]>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/selectivizr/1.0.2/selectivizr-min.js" integrity="sha512-0mXreXYrXoy9laHoypsAOjuSCqh57vY+kIdE46k8Hw0yRY1EhJyHWUEAqfHOTrPkBpsbO39/ZPw5HITv8mLVOA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
  <![endif]-->
  <title>Document</title>
</head>

<body>
  <div class="wrapper">
    <div class="box">
      <div class="input-box">
        <div class="upload-area-title">File</div>
        <form action="">
          <input type="file" name="upload" accept=".pdf,.jpg,.png,.jpeg" hidden>
          <label for="upload" class="uploadlabel">
            <span><i class="fa-solid fa-upload"></i></span>
            <p>click</p>
          </label>
        </form>
      </div>
      <div class="filewrapper">
        <h3 class="uploaded">Files</h3>
      </div>
    </div>
  </div>
  <script src="files/test.js"></script>
</body>

</html>
7tofc5zh

7tofc5zh1#

问题是标签(您看到的上传图标)被设置为for=“upload”。这意味着它链接到id=“upload”的输入。
但是,输入有一个name=“upload”,但没有ID。
如果您将其添加到输入的属性中,则会出现文件选择弹出窗口。

window.addEventListener("upload", () => {
  const input = document.getElementById("upload");
  const filewrapper = document.getElementById("filewrapper");

  input.addEventListener("change", (e) => {
    let fileName = e.target.files[0].name;
    let filetype = e.target.value.split(".").pop();
    fileshow(fileName, filetype);
  })

  const fileshow = (fileName, filetype) => {
    const showfileboxElem = document.createElement("div");
    showfileboxElem.classList.add("showfilebox");
    const leftElem = document.createElement("div");
    leftElem.classList.add("left");
    const fileTypeElem = document.createElement("span");
    fileTypeElem.classList.add("filetype")
    fileTypeElem.innerHTML = filetype;
    leftElem.append(fileTypeElem);
    const filetitleElem = document.createElement("h3");
    filetitleElem.innerHTML = fileName;
    leftElem.append(filetitleElem);
    showfileboxElem.append(leftElem);
    const rightElem = document.createElement("div");
    rightElem.classList.add("right");
    showfileboxElem.append(rightElem);
    const crossElem = document.createElement("span");
    crossElem.innerHTML = "&#215;";
    rightElem.append(crossElem);
    filewrapper.append(showfileboxElem);

    crossElem.addEventListener("click", () => {
      filewrapper.removeChild(showfileboxElem);
    })
  }
})
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: sans-serif;
}

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 15px;
  width: 100%;
  min-height: 100vh;
  background-color: rebeccapurple;
}

.box {
  max-width: 500px;
  background: #fff;
  padding: 30px;
  width: 100%;
  border-radius: 5px;
}

.upload-area-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 20px;
  font-weight: 600;
}

.uploadlabel {
  width: 100%;
  min-height: 100px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 3px dashed aqua;
  cursor: pointer;
}

.uploadlabel span {
  font-size: 70px;
  color: aqua;
}

.uploadlabel p {
  color: aqua;
  font-size: 20px;
  font-weight: 800;
  font-family: cursive;
}

.uploaded {
  margin: 30px 0;
  font-size: 16px;
  font-weight: 700;
  color: #222;
}

.showfilebox {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 10px 0;
  padding: 10px 15px;
  box-shadow: #00000d 0px 0px 0px 1px, #d1d5db3d 0px 0px 0px 1px inset;
}

.showfilebox .left {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.filetype {
  background: aqua;
  color: #fff;
  padding: 5px 15px;
  font-size: 20px;
  text-transform: capitalize;
  font-weight: 700;
  border-radius: 3px;
}

.left h3 {
  font-weight: 600;
  font-size: 18px;
  color: yellowgreen;
  margin: 0;
}

.right span {
  background: aqua;
  color: #fff;
  width: 25px;
  height: 25px;
  font-size: 25px;
  line-height: 25px;
  display: inline-block;
  text-align: center;
  font-weight: 700;
  cursor: pointer;
  border-radius: 50%;
}
<!DOCTYPE html>
<html lang="cs">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="description" content="" />
  <meta name="keywords" content="" />
  <meta name="author" content="Michal Kozumplík" />
  <link rel="stylesheet" href="" />
  <link rel="stylesheet" href="" />
  <link rel="stylesheet" href="files/style.css" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://kit.fontawesome.com/d8714c9903.js" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.2/jquery.min.js" integrity="sha512-tWHlutFnuG0C6nQRlpvrEhE4QpkG1nn2MOUMWmUeRePl4e3Aki0VB6W1v3oLjFtd0hVOtRQ9PHpSfN6u6/QXkQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  <!--[if lt IE 9]>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/selectivizr/1.0.2/selectivizr-min.js" integrity="sha512-0mXreXYrXoy9laHoypsAOjuSCqh57vY+kIdE46k8Hw0yRY1EhJyHWUEAqfHOTrPkBpsbO39/ZPw5HITv8mLVOA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
  <![endif]-->
  <title>Document</title>
</head>

<body>
  <div class="wrapper">
    <div class="box">
      <div class="input-box">
        <div class="upload-area-title">File</div>
        <form action="">
          <!-- **** added id="upload" to this i**** -->
          <input type="file" name="upload" accept=".pdf,.jpg,.png,.jpeg" hidden id="upload">
          <label for="upload" class="uploadlabel">
            <span><i class="fa-solid fa-upload"></i></span>
            <p>click</p>
          </label>
        </form>
      </div>
      <div class="filewrapper">
        <h3 class="uploaded">Files</h3>
      </div>
    </div>
  </div>
  <script src="files/test.js"></script>
</body>

</html>

相关问题