简体   繁体   English

如何在 JavaScript 中将多个选定的图像文件名转换为逗号分隔的字符串?

[英]How to convert multiple selected image filenames to a comma separated string in JavasScript?

I have an HTML form where a user can enter basic data about themselves.我有一个 HTML 表格,用户可以在其中输入有关自己的基本数据。 The last field is of input type="file" .最后一个字段是input type="file" The user can select multiple images from their local disk drive.用户可以从他们的本地磁盘驱动器 select 多个图像。

I want to store the images in one string with the selected images separated by commas.我想将图像存储在一个字符串中,所选图像用逗号分隔。

Here is my code which is not working:这是我的代码不起作用:

 var input = document.mainForm.imageInput; input.onchange = function() { var file = input.files[0], reader = new FileReader(); reader.onloadend = function() { var filesSize = 0 for (i = 0; i < input.files.length; i++) { filesSize += input.files[i].size } if ((filesSize / 1024 / 1024) > 100) { console.log("bigger"); } else { console.log("smaller"); * // I need to do a for loop here to save all images in one string in document.mainForm.hiddenImageNameBase64.value * var b64Transformation = reader.result.replace(/^data:.+;base64,/, ''); console.log(b64Transformation); document.mainForm.hiddenImageNameBase64.value = b64Transformation; var allImagesInOneString = "" for (i = 0; i < input.files.length; i++) { allImagesInOneString += input.files[i].value.replace(/.*[\/\\]/, ''); } document.mainForm.imageName.value = allImagesInOneString; input.classList.remove("is-ok"); input.classList.add("is-notok"); } }; reader.readAsDataURL(file); };
 <div class="container"> <form class="x" name="mainForm" id="mainForm" method="post" action="https://testpagex.com"> <div class="form-row"> <div class="col-md-6 mb-3"> <label for="imageInput" class="font-weight-bold">Add images ( maximum size 100mb )</label> <input type="file" class="form-control-file" id="imageInput" name="imageInput" multiple /> </div> </div> <input type="hidden" name="hiddenImageName"> <input type="hidden" name="hiddenImageNameBase64"> <div class="y"> <button type="submit" class="btn btn-primary px-5 font-weight-bold">Save</button> </div> </form> </div>

This seems like an XY Problem , but I'm going to work this anyway, since I think I have solutions to help you in multiple ways.这似乎是一个XY 问题,但无论如何我都会这样做,因为我认为我有解决方案可以以多种方式帮助您。

First, to answer the question you have for putting multiple images in a single comma delimited string, I'd think that creating a string array would be the easiest way to go.首先,要回答将多个图像放在单个逗号分隔字符串中的问题,我认为创建字符串数组将是 go 的最简单方法。 You just create your base64 strings individually and add them to the array.您只需单独创建 base64 字符串并将它们添加到数组中。 Then you can Join the array elements together with a comma and get the single string you're looking for.然后你可以用逗号将数组元素连接在一起,得到你正在寻找的单个字符串。

I'm not sure why you want to do this, since it seems unnecessary.我不确定您为什么要这样做,因为这似乎没有必要。

My second option is to leave it as a string array.我的第二个选择是将其保留为字符串数组。 I'm assuming you are sending this data to a backend server-side script/app/service/whatever to save.我假设您正在将此数据发送到后端服务器端脚本/应用程序/服务/要保存的任何内容。 That server side code will be able to handle the string array just fine.该服务器端代码将能够很好地处理字符串数组。 Then you can save it to the DB or a file(s).然后您可以将其保存到数据库或文件中。

My third option springboards off the second.我的第三个选项跳板从第二个。 Instead of saving the data to a single column/row in the DB or as a single file on your server, save the images individually.不要将数据保存到数据库中的单个列/行或服务器上的单个文件,而是单独保存图像。 Each image goes into it's own file or row in your DB.每个图像进入它自己的文件或数据库中的行。 This probably means creating a new table in your DB just for images and linking them to whatever table and row you are saving the rest of your data.这可能意味着在您的数据库中为图像创建一个新表,并将它们链接到您保存数据的 rest 的任何表和行。 This may also be necessary if you are saving filenames of the files you are creating in the DB.如果您要保存在数据库中创建的文件的文件名,这也可能是必需的。

I understand that you may not have the access or privileges to add tables to your DB, but it's at least something to consider if you do have that ability.我知道您可能没有将表添加到数据库的访问权限或特权,但如果您确实有这种能力,至少需要考虑一下。 Keeping the images separate can help you avoid the complication of trying to determine if a string is a single or multiple images.将图像分开可以帮助您避免尝试确定字符串是单个图像还是多个图像的复杂性。 Fortunately, Base64 characters don't include the comma.幸运的是, Base64字符不包含逗号。

Also, I'm not sure why you are bothering to do this at all.另外,我不确定你为什么要费心去做这件事。 Uploading multiple files to the server should be something your server side language can handle.将多个文件上传到服务器应该是您的服务器端语言可以处理的。 Browsers can do this without you having to do it manually, like you are.浏览器可以做到这一点,而无需像您一样手动进行。 Then again, you may be stuck doing it your way due to not having access to edit the code of an API you are using.再说一次,由于无法编辑您正在使用的 API 的代码,您可能会被困在自己的方式中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一串逗号分隔的数字转换为整数? - How to convert a string of comma separated numbers to integers? 如何在 JavaScript 中将逗号分隔的字符串转换为数组 - How to Convert Comma Separated String into an Array in JavaScript 如何在 JavaScript 中解析一串键/值对,对以逗号分隔? - How to parse a string of key/value pairs in JavasScript, with pairs separated by commas? 如何安全地将逗号分隔的字符串转换为数字数组 - How to safely convert a comma-separated string to an array of numbers 如何将逗号分隔的查询字符串转换为字符串数组 - How can i convert comma separated query String to a Array of Strings 如何将逗号分隔的字符串转换为数组? - How can I convert a comma-separated string to an array? 如何将逗号分隔的字符串转换为数据数组? - How to convert a string of comma separated values into data array? 如何在 ReactJS 中将二维数组转换为逗号分隔的字符串 - How to convert 2D array in to comma separated string in ReactJS 如何在JavaScript中将本地存储的逗号分隔字符串转换为数值数组 - How to convert comma separated string of local storage into numeric array in javascript 如何将嵌套的数组数组转换为逗号分隔的字符串 - how to convert nested array of arrays into comma separated string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM