上传多张图片并将它们转换为base64?

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

Uploading multiple images and convert them to base64?

问题

I can help with the translation of the code. Here's the translated code:

  1. <div class="form-group">
  2. <label for="product_photo_1">照片 1</label>
  3. <div class="custom-file">
  4. <input type="file" class="custom-file-input" id="product_photo_1" multiple />
  5. <label class="custom-file-label" for="product_photo_1" data-browse="选择">请选择文件...</label>
  6. </div>
  7. <div id="product_photo_1_result"></div>
  8. </div>
  9. <div class="form-group">
  10. <label for="product_photo_2">照片 2</label>
  11. <div class="custom-file">
  12. <input type="file" class="custom-file-input" id="product_photo_2" multiple />
  13. <label class="custom-file-label" for="product_photo_2" data-browse="选择">请选择文件...</label>
  14. </div>
  15. <div id="product_photo_2_result"></div>
  16. </div>
  17. <script>
  18. const convertBase64 = (file) => {
  19. return new Promise((resolve, reject) => {
  20. const fileReader = new FileReader();
  21. fileReader.readAsDataURL(file);
  22. fileReader.onload = () => {
  23. resolve(fileReader.result);
  24. };
  25. fileReader.onerror = (error) => {
  26. reject(error);
  27. };
  28. });
  29. };
  30. const uploadImage = async (event, id) => {
  31. const file = event.target.files[0];
  32. const base64 = await convertBase64(file);
  33. document.getElementById("product_photo_" + id + "_result").innerHTML += '<img src="' + base64 + '" class="img-thumbnail m-2" id="product_photo_' + id + '[]" style="height: 50px;" />';
  34. };
  35. document.getElementById("product_photo_1").addEventListener("change", (event) => {
  36. for (var index = 1; index <= document.getElementById("product_photo_1").files.length; index++) {
  37. uploadImage(event, 1);
  38. }
  39. });
  40. document.getElementById("product_photo_2").addEventListener("change", (event) => {
  41. for (var index = 1; index <= document.getElementById("product_photo_2").files.length; index++) {
  42. uploadImage(event, 2);
  43. }
  44. });
  45. </script>

This code appears to be related to creating a form for uploading multiple images and displaying them as base64 encoded images using jQuery's POST.

英文:

I'm trying to create a form where you can upload multiple images using two file inputs, see the images as base64 and later submit them using jQuery's POST. The problem is that when I select 3 different images, I see only the last one 3 times. I guess the problem is in the for in the addEventListener - maybe it's not in the right place?

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

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

  1. &lt;div class=&quot;form-group&quot;&gt;
  2. &lt;label for=&quot;product_photo_1&quot;&gt;Photos 1&lt;/label&gt;
  3. &lt;div class=&quot;custom-file&quot;&gt;
  4. &lt;input type=&quot;file&quot; class=&quot;custom-file-input&quot; id=&quot;product_photo_1&quot; multiple /&gt;
  5. &lt;label class=&quot;custom-file-label&quot; for=&quot;product_photo_1&quot; data-browse=&quot;Select&quot;&gt;Please, select a file...&lt;/label&gt;
  6. &lt;/div&gt;
  7. &lt;div id=&quot;product_photo_1_result&quot;&gt;&lt;/div&gt;
  8. &lt;/div&gt;
  9. &lt;div class=&quot;form-group&quot;&gt;
  10. &lt;label for=&quot;product_photo_2&quot;&gt;Photos 2&lt;/label&gt;
  11. &lt;div class=&quot;custom-file&quot;&gt;
  12. &lt;input type=&quot;file&quot; class=&quot;custom-file-input&quot; id=&quot;product_photo_2&quot; multiple /&gt;
  13. &lt;label class=&quot;custom-file-label&quot; for=&quot;product_photo_2&quot; data-browse=&quot;Select&quot;&gt;Please, select a file...&lt;/label&gt;
  14. &lt;/div&gt;
  15. &lt;div id=&quot;product_photo_2_result&quot;&gt;&lt;/div&gt;
  16. &lt;/div&gt;
  17. &lt;script&gt;
  18. const convertBase64 = (file) =&gt; {
  19. return new Promise((resolve, reject) =&gt; {
  20. const fileReader = new FileReader();
  21. fileReader.readAsDataURL(file);
  22. fileReader.onload = () =&gt; {
  23. resolve(fileReader.result);
  24. };
  25. fileReader.onerror = (error) =&gt; {
  26. reject(error);
  27. };
  28. });
  29. };
  30. const uploadImage = async (event, id) =&gt; {
  31. const file = event.target.files[0];
  32. const base64 = await convertBase64(file);
  33. document.getElementById(&quot;product_photo_&quot; + id + &quot;_result&quot;).innerHTML += &#39;&lt;img src=&quot;&#39; + base64 + &#39;&quot; class=&quot;img-thumbnail m-2&quot; id=&quot;product_photo_&#39; + id + &#39;[]&quot; style=&quot;height: 50px;&quot; /&gt;&#39;;
  34. };
  35. document.getElementById(&quot;product_photo_1&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {
  36. for(var index = 1; index &lt;= document.getElementById(&quot;product_photo_1&quot;).files.length; index++) {
  37. uploadImage(event, 1);
  38. }
  39. });
  40. document.getElementById(&quot;product_photo_2&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {
  41. for(var index = 1; index &lt;= document.getElementById(&quot;product_photo_2&quot;).files.length; index++) {
  42. uploadImage(event, 2);
  43. }
  44. });
  45. &lt;/script&gt;

<!-- end snippet -->

答案1

得分: 2

uploadImage 函数中,你每次都传递了 event 参数并选择了第一个文件

  1. const file = event.target.files[0];

相反,你可以将文件逐个传递给 uploadImage 函数。

这里是示例:

  1. <div class="form-group">
  2. <label for="product_photo_1">Photos 1</label>
  3. <div class="custom-file">
  4. <input type="file" class="custom-file-input" id="product_photo_1" multiple />
  5. <label class="custom-file-label" for="product_photo_1" data-browse="Select">Please, select a file...</label>
  6. </div>
  7. <div id="product_photo_1_result"></div>
  8. </div>
  9. <div class="form-group">
  10. <label for="product_photo_2">Photos 2</label>
  11. <div class="custom-file">
  12. <input type="file" class="custom-file-input" id="product_photo_2" multiple />
  13. <label class="custom-file-label" for="product_photo_2" data-browse="Select">Please, select a file...</label>
  14. </div>
  15. <div id="product_photo_2_result"></div>
  16. </div>
  17. <script>
  18. const convertBase64 = (file) => {
  19. return new Promise((resolve, reject) => {
  20. const fileReader = new FileReader();
  21. fileReader.readAsDataURL(file);
  22. fileReader.onload = () => {
  23. resolve(fileReader.result);
  24. };
  25. fileReader.onerror = (error) => {
  26. reject(error);
  27. };
  28. });
  29. };
  30. const uploadImage = async (file, id) => {
  31. const base64 = await convertBase64(file);
  32. document.getElementById("product_photo_" + id + "_result").innerHTML += '<img src="' + base64 + '" class="img-thumbnail m-2" id="product_photo_' + id + '[]" style="height: 50px;" />';
  33. };
  34. document.getElementById("product_photo_1").addEventListener("change", (event) => {
  35. let files = document.getElementById("product_photo_1").files;
  36. for (let index = 0; index <= files.length; index++) {
  37. uploadImage(files[index], 1);
  38. }
  39. });
  40. document.getElementById("product_photo_2").addEventListener("change", (event) => {
  41. let files = document.getElementById("product_photo_2").files;
  42. for (let index = 0; index <= document.getElementById("product_photo_2").files.length; index++) {
  43. uploadImage(files[index], 2);
  44. }
  45. });
  46. </script>
英文:

In uploadImage function you are passing event argument and select first file every time

  1. const file = event.target.files[0];

Instead you can pass files one by one to the uploadImage.

Here is the example:

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

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

  1. &lt;div class=&quot;form-group&quot;&gt;
  2. &lt;label for=&quot;product_photo_1&quot;&gt;Photos 1&lt;/label&gt;
  3. &lt;div class=&quot;custom-file&quot;&gt;
  4. &lt;input type=&quot;file&quot; class=&quot;custom-file-input&quot; id=&quot;product_photo_1&quot; multiple /&gt;
  5. &lt;label class=&quot;custom-file-label&quot; for=&quot;product_photo_1&quot; data-browse=&quot;Select&quot;&gt;Please, select a file...&lt;/label&gt;
  6. &lt;/div&gt;
  7. &lt;div id=&quot;product_photo_1_result&quot;&gt;&lt;/div&gt;
  8. &lt;/div&gt;
  9. &lt;div class=&quot;form-group&quot;&gt;
  10. &lt;label for=&quot;product_photo_2&quot;&gt;Photos 2&lt;/label&gt;
  11. &lt;div class=&quot;custom-file&quot;&gt;
  12. &lt;input type=&quot;file&quot; class=&quot;custom-file-input&quot; id=&quot;product_photo_2&quot; multiple /&gt;
  13. &lt;label class=&quot;custom-file-label&quot; for=&quot;product_photo_2&quot; data-browse=&quot;Select&quot;&gt;Please, select a file...&lt;/label&gt;
  14. &lt;/div&gt;
  15. &lt;div id=&quot;product_photo_2_result&quot;&gt;&lt;/div&gt;
  16. &lt;/div&gt;
  17. &lt;script&gt;
  18. const convertBase64 = (file) =&gt; {
  19. return new Promise((resolve, reject) =&gt; {
  20. const fileReader = new FileReader();
  21. fileReader.readAsDataURL(file);
  22. fileReader.onload = () =&gt; {
  23. resolve(fileReader.result);
  24. };
  25. fileReader.onerror = (error) =&gt; {
  26. reject(error);
  27. };
  28. });
  29. };
  30. const uploadImage = async (file, id) =&gt; {
  31. const base64 = await convertBase64(file);
  32. document.getElementById(&quot;product_photo_&quot; + id + &quot;_result&quot;).innerHTML += &#39;&lt;img src=&quot;&#39; + base64 + &#39;&quot; class=&quot;img-thumbnail m-2&quot; id=&quot;product_photo_&#39; + id + &#39;[]&quot; style=&quot;height: 50px;&quot; /&gt;&#39;;
  33. };
  34. document.getElementById(&quot;product_photo_1&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {
  35. let files = document.getElementById(&quot;product_photo_1&quot;).files;
  36. for (let index = 0; index &lt;= files.length; index++) {
  37. uploadImage(files[index], 1);
  38. }
  39. });
  40. document.getElementById(&quot;product_photo_2&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {
  41. let files = document.getElementById(&quot;product_photo_2&quot;).files;
  42. for (let index = 0; index &lt;= document.getElementById(&quot;product_photo_2&quot;).files.length; index++) {
  43. uploadImage(files[index], 2);
  44. }
  45. });
  46. &lt;/script&gt;

<!-- end snippet -->

答案2

得分: 0

In the event handlers, you are doing a loop, but you are not using the value of index per iteration:

  1. document.getElementById("product_photo_1").addEventListener("change", (event) => {
  2. for (var index = 1; index <= document.getElementById("product_photo_1").files.length; index++) {
  3. uploadImage(event, 1); //passing only the event and an index
  4. }
  5. });
  6. document.getElementById("product_photo_2").addEventListener("change", (event) => {
  7. for (var index = 1; index <= document.getElementById("product_photo_2").files.length; index++) {
  8. uploadImage(event, 2); //passing only the event and the index
  9. }
  10. });

And now, let's use index for all calls as well as clearing up when it's 0:

  1. const uploadImage = async (event, id, index) => {
  2. let context = document.getElementById("product_photo_" + id + "_result");
  3. const file = event.target.files[index]; //we get the correct file;
  4. const base64 = await convertBase64(file);
  5. let template = '<img src="' + base64 + '" class="img-thumbnail m-2" id="product_photo_' + id + '[]" style="height: 50px;" />';
  6. if (index === 0) context.innerHTML = template; //clearing older values and adding the first element
  7. else context.innerHTML += template; //appending the (not first) element
  8. };

Instead, you will need to pass the index as well, like this:

  1. document.getElementById("product_photo_1").addEventListener("change", (event) => {
  2. for (var index = 0; index < document.getElementById("product_photo_1").files.length; index++) {
  3. uploadImage(event, 1, index);
  4. }
  5. });
  6. document.getElementById("product_photo_2").addEventListener("change", (event) => {
  7. for (var index = 0; index < document.getElementById("product_photo_2").files.length; index++) {
  8. uploadImage(event, 2, index);
  9. }
  10. });
英文:

In the event handlers, you are doing a loop, but you are not using the value of index per iteration:

  1. document.getElementById(&quot;product_photo_1&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {
  2. for(var index = 1; index &lt;= document.getElementById(&quot;product_photo_1&quot;).files.length; index++) {
  3. uploadImage(event, 1); //passing only the event and an index
  4. }
  5. });
  6. document.getElementById(&quot;product_photo_2&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {
  7. for(var index = 1; index &lt;= document.getElementById(&quot;product_photo_2&quot;).files.length; index++) {
  8. uploadImage(event, 2); //passing only the event and the index
  9. }
  10. });
  11. And now, let&#39;s use `index` for all calls as well as clearing up when it&#39;s 0:
  1. const uploadImage = async (event, id, index) =&gt; {
  2. let context = document.getElementById(&quot;product_photo_&quot; + id + &quot;_result&quot;);
  3. const file = event.target.files[index]; //we get the correct file;
  4. const base64 = await convertBase64(file);
  5. let template = &#39;&lt;img src=&quot;&#39; + base64 + &#39;&quot; class=&quot;img-thumbnail m-2&quot; id=&quot;product_photo_&#39; + id + &#39;[]&quot; style=&quot;height: 50px;&quot; /&gt;&#39;;
  6. if (index === 0) context.innerHTML = template; //clearing older values and adding the first element
  7. else context.innerHTML += template; //appending the (not first) element
  8. };

Instead, you will need to pass the index as well, like this:

  1. document.getElementById(&quot;product_photo_1&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {
  2. for(var index = 0; index &lt; document.getElementById(&quot;product_photo_1&quot;).files.length; index++) {
  3. uploadImage(event, 1, index);
  4. }
  5. });
  6. document.getElementById(&quot;product_photo_2&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {
  7. for(var index = 0; index &lt; document.getElementById(&quot;product_photo_2&quot;).files.length; index++) {
  8. uploadImage(event, 2, index);
  9. }
  10. });

huangapple
  • 本文由 发表于 2023年5月10日 21:03:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218760.html
匿名

发表评论

匿名网友

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

确定