文件上传区域

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

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 -->

  1. window.addEventListener(&quot;upload&quot;, ()=&gt;{
  2. const input = document.getElementById(&quot;upload&quot;);
  3. const filewrapper = document.getElementById(&quot;filewrapper&quot;);
  4. input.addEventListener(&quot;change&quot;,(e)=&gt;{
  5. let fileName = e.target.files[0].name;
  6. let filetype = e.target.value.split(&quot;.&quot;).pop();
  7. fileshow(fileName, filetype);
  8. })
  9. const fileshow=(fileName, filetype)=&gt;{
  10. const showfileboxElem = document.createElement(&quot;div&quot;);
  11. showfileboxElem.classList.add(&quot;showfilebox&quot;);
  12. const leftElem = document.createElement(&quot;div&quot;);
  13. leftElem.classList.add(&quot;left&quot;);
  14. const fileTypeElem = document.createElement(&quot;span&quot;);
  15. fileTypeElem.classList.add(&quot;filetype&quot;)
  16. fileTypeElem.innerHTML=filetype;
  17. leftElem.append(fileTypeElem);
  18. const filetitleElem = document.createElement(&quot;h3&quot;);
  19. filetitleElem.innerHTML=fileName;
  20. leftElem.append(filetitleElem);
  21. showfileboxElem.append(leftElem);
  22. const rightElem = document.createElement(&quot;div&quot;);
  23. rightElem.classList.add(&quot;right&quot;);
  24. showfileboxElem.append(rightElem);
  25. const crossElem = document.createElement(&quot;span&quot;);
  26. crossElem.innerHTML=&quot;&amp;#215;&quot;;
  27. rightElem.append(crossElem);
  28. filewrapper.append(showfileboxElem);
  29. crossElem.addEventListener(&quot;click&quot;, ()=&gt;{
  30. filewrapper.removeChild(showfileboxElem);
  31. })
  32. }
  33. })

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

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. font-family: sans-serif;
  6. }
  7. .wrapper {
  8. display: flex;
  9. justify-content: center;
  10. align-items: center;
  11. padding: 15px;
  12. width: 100%;
  13. min-height: 100vh;
  14. background-color: rebeccapurple;
  15. }
  16. .box {
  17. max-width: 500px;
  18. background: #fff;
  19. padding: 30px;
  20. width: 100%;
  21. border-radius: 5px;
  22. }
  23. .upload-area-title {
  24. text-align: center;
  25. margin-bottom: 20px;
  26. font-size: 20px;
  27. font-weight: 600;
  28. }
  29. .uploadlabel {
  30. width: 100%;
  31. min-height: 100px;
  32. background-color: #fff;
  33. display: flex;
  34. flex-direction: column;
  35. justify-content: center;
  36. align-items: center;
  37. border: 3px dashed aqua;
  38. cursor: pointer;
  39. }
  40. .uploadlabel span {
  41. font-size: 70px;
  42. color: aqua;
  43. }
  44. .uploadlabel p {
  45. color: aqua;
  46. font-size: 20px;
  47. font-weight: 800;
  48. font-family: cursive;
  49. }
  50. .uploaded {
  51. margin: 30px 0;
  52. font-size: 16px;
  53. font-weight: 700;
  54. color: #222;
  55. }
  56. .showfilebox {
  57. display: flex;
  58. align-items: center;
  59. justify-content: space-between;
  60. margin: 10px 0;
  61. padding: 10px 15px;
  62. box-shadow: #00000d 0px 0px 0px 1px, #d1d5db3d 0px 0px 0px 1px inset;
  63. }
  64. .showfilebox .left {
  65. display: flex;
  66. align-items: center;
  67. flex-wrap: wrap;
  68. gap: 10px;
  69. }
  70. .filetype {
  71. background: aqua;
  72. color: #fff;
  73. padding: 5px 15px;
  74. font-size: 20px;
  75. text-transform: capitalize;
  76. font-weight: 700;
  77. border-radius: 3px;
  78. }
  79. .left h3 {
  80. font-weight: 600;
  81. font-size: 18px;
  82. color: yellowgreen;
  83. margin: 0;
  84. }
  85. .right span {
  86. background: aqua;
  87. color: #fff;
  88. width: 25px;
  89. height: 25px;
  90. font-size: 25px;
  91. line-height: 25px;
  92. display: inline-block;
  93. text-align: center;
  94. font-weight: 700;
  95. cursor: pointer;
  96. border-radius: 50%;
  97. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;cs&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot; /&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  7. &lt;meta name=&quot;description&quot; content=&quot;&quot; /&gt;
  8. &lt;meta name=&quot;keywords&quot; content=&quot;&quot; /&gt;
  9. &lt;link rel=&quot;stylesheet&quot; href=&quot;&quot; /&gt;
  10. &lt;link rel=&quot;stylesheet&quot; href=&quot;&quot; /&gt;
  11. &lt;link rel=&quot;stylesheet&quot; href=&quot;files/style.css&quot; /&gt;
  12. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
  13. &lt;script src=&quot;https://kit.fontawesome.com/d8714c9903.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  14. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.2/jquery.min.js&quot;
  15. integrity=&quot;sha512-tWHlutFnuG0C6nQRlpvrEhE4QpkG1nn2MOUMWmUeRePl4e3Aki0VB6W1v3oLjFtd0hVOtRQ9PHpSfN6u6/QXkQ==&quot;
  16. crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
  17. &lt;!--[if lt IE 9]&gt;
  18. &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;
  19. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js&quot;&gt;&lt;/script&gt;
  20. &lt;![endif]--&gt;
  21. &lt;title&gt;Document&lt;/title&gt;
  22. &lt;/head&gt;
  23. &lt;body&gt;
  24. &lt;div class=&quot;wrapper&quot;&gt;
  25. &lt;div class=&quot;box&quot;&gt;
  26. &lt;div class=&quot;input-box&quot;&gt;
  27. &lt;div class=&quot;upload-area-title&quot;&gt;File&lt;/div&gt;
  28. &lt;form action=&quot;&quot;&gt;
  29. &lt;input type=&quot;file&quot; name=&quot;upload&quot; accept=&quot;.pdf,.jpg,.png,.jpeg&quot; hidden&gt;
  30. &lt;label for=&quot;upload&quot; class=&quot;uploadlabel&quot;&gt;
  31. &lt;span&gt;&lt;i class=&quot;fa-solid fa-upload&quot;&gt;&lt;/i&gt;&lt;/span&gt;
  32. &lt;p&gt;click&lt;/p&gt;
  33. &lt;/label&gt;
  34. &lt;/form&gt;
  35. &lt;/div&gt;
  36. &lt;div class=&quot;filewrapper&quot;&gt;
  37. &lt;h3 class=&quot;uploaded&quot;&gt;Files&lt;/h3&gt;
  38. &lt;/div&gt;
  39. &lt;/div&gt;
  40. &lt;/div&gt;
  41. &lt;script src=&quot;files/test.js&quot;&gt;&lt;/script&gt;
  42. &lt;/body&gt;
  43. &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 -->

  1. window.addEventListener(&quot;upload&quot;, () =&gt; {
  2. const input = document.getElementById(&quot;upload&quot;);
  3. const filewrapper = document.getElementById(&quot;filewrapper&quot;);
  4. input.addEventListener(&quot;change&quot;, (e) =&gt; {
  5. let fileName = e.target.files[0].name;
  6. let filetype = e.target.value.split(&quot;.&quot;).pop();
  7. fileshow(fileName, filetype);
  8. })
  9. const fileshow = (fileName, filetype) =&gt; {
  10. const showfileboxElem = document.createElement(&quot;div&quot;);
  11. showfileboxElem.classList.add(&quot;showfilebox&quot;);
  12. const leftElem = document.createElement(&quot;div&quot;);
  13. leftElem.classList.add(&quot;left&quot;);
  14. const fileTypeElem = document.createElement(&quot;span&quot;);
  15. fileTypeElem.classList.add(&quot;filetype&quot;)
  16. fileTypeElem.innerHTML = filetype;
  17. leftElem.append(fileTypeElem);
  18. const filetitleElem = document.createElement(&quot;h3&quot;);
  19. filetitleElem.innerHTML = fileName;
  20. leftElem.append(filetitleElem);
  21. showfileboxElem.append(leftElem);
  22. const rightElem = document.createElement(&quot;div&quot;);
  23. rightElem.classList.add(&quot;right&quot;);
  24. showfileboxElem.append(rightElem);
  25. const crossElem = document.createElement(&quot;span&quot;);
  26. crossElem.innerHTML = &quot;&amp;#215;&quot;;
  27. rightElem.append(crossElem);
  28. filewrapper.append(showfileboxElem);
  29. crossElem.addEventListener(&quot;click&quot;, () =&gt; {
  30. filewrapper.removeChild(showfileboxElem);
  31. })
  32. }
  33. })

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

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. font-family: sans-serif;
  6. }
  7. .wrapper {
  8. display: flex;
  9. justify-content: center;
  10. align-items: center;
  11. padding: 15px;
  12. width: 100%;
  13. min-height: 100vh;
  14. background-color: rebeccapurple;
  15. }
  16. .box {
  17. max-width: 500px;
  18. background: #fff;
  19. padding: 30px;
  20. width: 100%;
  21. border-radius: 5px;
  22. }
  23. .upload-area-title {
  24. text-align: center;
  25. margin-bottom: 20px;
  26. font-size: 20px;
  27. font-weight: 600;
  28. }
  29. .uploadlabel {
  30. width: 100%;
  31. min-height: 100px;
  32. background-color: #fff;
  33. display: flex;
  34. flex-direction: column;
  35. justify-content: center;
  36. align-items: center;
  37. border: 3px dashed aqua;
  38. cursor: pointer;
  39. }
  40. .uploadlabel span {
  41. font-size: 70px;
  42. color: aqua;
  43. }
  44. .uploadlabel p {
  45. color: aqua;
  46. font-size: 20px;
  47. font-weight: 800;
  48. font-family: cursive;
  49. }
  50. .uploaded {
  51. margin: 30px 0;
  52. font-size: 16px;
  53. font-weight: 700;
  54. color: #222;
  55. }
  56. .showfilebox {
  57. display: flex;
  58. align-items: center;
  59. justify-content: space-between;
  60. margin: 10px 0;
  61. padding: 10px 15px;
  62. box-shadow: #00000d 0px 0px 0px 1px, #d1d5db3d 0px 0px 0px 1px inset;
  63. }
  64. .showfilebox .left {
  65. display: flex;
  66. align-items: center;
  67. flex-wrap: wrap;
  68. gap: 10px;
  69. }
  70. .filetype {
  71. background: aqua;
  72. color: #fff;
  73. padding: 5px 15px;
  74. font-size: 20px;
  75. text-transform: capitalize;
  76. font-weight: 700;
  77. border-radius: 3px;
  78. }
  79. .left h3 {
  80. font-weight: 600;
  81. font-size: 18px;
  82. color: yellowgreen;
  83. margin: 0;
  84. }
  85. .right span {
  86. background: aqua;
  87. color: #fff;
  88. width: 25px;
  89. height: 25px;
  90. font-size: 25px;
  91. line-height: 25px;
  92. display: inline-block;
  93. text-align: center;
  94. font-weight: 700;
  95. cursor: pointer;
  96. border-radius: 50%;
  97. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;cs&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot; /&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  7. &lt;meta name=&quot;description&quot; content=&quot;&quot; /&gt;
  8. &lt;meta name=&quot;keywords&quot; content=&quot;&quot; /&gt;
  9. &lt;meta name=&quot;author&quot; content=&quot;Michal Kozumpl&#237;k&quot; /&gt;
  10. &lt;link rel=&quot;stylesheet&quot; href=&quot;&quot; /&gt;
  11. &lt;link rel=&quot;stylesheet&quot; href=&quot;&quot; /&gt;
  12. &lt;link rel=&quot;stylesheet&quot; href=&quot;files/style.css&quot; /&gt;
  13. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
  14. &lt;script src=&quot;https://kit.fontawesome.com/d8714c9903.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  15. &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;
  16. &lt;!--[if lt IE 9]&gt;
  17. &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;
  18. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js&quot;&gt;&lt;/script&gt;
  19. &lt;![endif]--&gt;
  20. &lt;title&gt;Document&lt;/title&gt;
  21. &lt;/head&gt;
  22. &lt;body&gt;
  23. &lt;div class=&quot;wrapper&quot;&gt;
  24. &lt;div class=&quot;box&quot;&gt;
  25. &lt;div class=&quot;input-box&quot;&gt;
  26. &lt;div class=&quot;upload-area-title&quot;&gt;File&lt;/div&gt;
  27. &lt;form action=&quot;&quot;&gt;
  28. &lt;!-- **** added id=&quot;upload&quot; to this i**** --&gt;
  29. &lt;input type=&quot;file&quot; name=&quot;upload&quot; accept=&quot;.pdf,.jpg,.png,.jpeg&quot; hidden id=&quot;upload&quot;&gt;
  30. &lt;label for=&quot;upload&quot; class=&quot;uploadlabel&quot;&gt;
  31. &lt;span&gt;&lt;i class=&quot;fa-solid fa-upload&quot;&gt;&lt;/i&gt;&lt;/span&gt;
  32. &lt;p&gt;click&lt;/p&gt;
  33. &lt;/label&gt;
  34. &lt;/form&gt;
  35. &lt;/div&gt;
  36. &lt;div class=&quot;filewrapper&quot;&gt;
  37. &lt;h3 class=&quot;uploaded&quot;&gt;Files&lt;/h3&gt;
  38. &lt;/div&gt;
  39. &lt;/div&gt;
  40. &lt;/div&gt;
  41. &lt;script src=&quot;files/test.js&quot;&gt;&lt;/script&gt;
  42. &lt;/body&gt;
  43. &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:

确定