文件上传区域

huangapple go评论60阅读模式
英文:

File upload area

问题

你好,以下是您的内容的翻译部分:

"Hello I have a problem with my code for uploading files on website. The problem is that I can not get the file. I mean that I can not upload file because the area where must be works it is not working. It is from video tutorial. Can someone look at it and say me where i have a problem? Thank you so much."

"你好,我在上传文件到网站的代码中遇到了问题。问题是我无法获取文件。我的意思是,我无法上传文件,因为应该工作的区域不起作用。这是来自视频教程的代码。有人可以看一下并告诉我哪里有问题吗?非常感谢。"

英文:

Hello I have a problem with my code for uploading files on website. The problem is that I can not get the file. I mean that I can not upload file because the area where must be works it is not working. It is from video tutorial. Can someone look at it and say me where i have a problem? Thank you so much.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.addEventListener(&quot;upload&quot;, ()=&gt;{
  const input = document.getElementById(&quot;upload&quot;);
  const filewrapper = document.getElementById(&quot;filewrapper&quot;);

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

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

    crossElem.addEventListener(&quot;click&quot;, ()=&gt;{
      filewrapper.removeChild(showfileboxElem);
    })
  }
})

<!-- language: lang-css -->

* {
  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%;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;cs&quot;&gt;

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

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

&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 0

以下是您提供的内容的翻译:

问题在于标签(即您看到的上传图标)设置为for="upload"。这意味着它与一个具有id="upload"的输入关联。

然而,该输入具有name="upload"但没有id。

如果您将id添加到输入的属性中,文件选择弹出窗口将出现。

英文:

The problem is that the label (which is the upload icon you see) is set as for="upload". That means it is linked to an input with id="upload".

However, the input has a name="upload" but no id.

If you add that to the input's attributes then the file selection popup appears.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.addEventListener(&quot;upload&quot;, () =&gt; {
  const input = document.getElementById(&quot;upload&quot;);
  const filewrapper = document.getElementById(&quot;filewrapper&quot;);

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

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

    crossElem.addEventListener(&quot;click&quot;, () =&gt; {
      filewrapper.removeChild(showfileboxElem);
    })
  }
})

<!-- language: lang-css -->

* {
  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%;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;cs&quot;&gt;

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

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

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 16:38:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定