Create your document library with the respective folders.

SharePoint
Create a txt file and add the below code (after making necessary changes at the highlighted areas, such as - your library name, folder name respectively) and save it in Site Assets.
  1. <html> <head> <script language="javascript" type="text/javascript" src="https://industowers.sharepoint.com/sites/LPMS/Style%20Library/LPMS/JS/1.8.1jquery.min.js">
  2. </script>
  3. <script language="javascript" type="text/javascript">
  4. var fileInput;
  5. $(document).ready(function()
  6. {
  7. fileInput = $("#getFile");
  8. SP.SOD.executeFunc('sp.js', 'SP.ClientContext', registerClick);
  9. });
  10. function registerClick()
  11. {
  12. //Register File Upload Click Event
  13. $("#addFileButton").click(readFile);
  14. }
  15. var arrayBuffer;
  16. function readFile()
  17. {
  18. //Get File Input Control and read th file name
  19. var element = document.getElementById("getFile");
  20. var file = element.files[0];
  21. var parts = element.value.split("\\");
  22. var fileName = parts[parts.length - 1];
  23. //Read File contents using file reader
  24. var reader = new FileReader();
  25. reader.onload = function(e) {
  26. uploadFile(e.target.result, fileName);
  27. }
  28. reader.onerror = function(e)
  29. {
  30. alert(e.target.error);
  31. }
  32. reader.readAsArrayBuffer(file);
  33. }
  34. var attachmentFiles, clientContext, createInfo;
  35. function uploadFile(arrayBuffer, fileName)
  36. {
  37. //Get Client Context and Web object.
  38. clientContext = new SP.ClientContext();
  39. var oWeb = clientContext.get_web();
  40. //Get list and Attachment folder where the attachment of a particular list item is stored.
  41. var oList = oWeb.get_lists().getByTitle('CS100');
  42. var attachmentFolder = oWeb.getFolderByServerRelativeUrl('/sites/LPMS/CS100/Trial');
  43. //Convert the file contents into base64 data
  44. var bytes = new Uint8Array(arrayBuffer);
  45. var i, length, out = '';
  46. for (i = 0, length = bytes.length; i < length; i += 1) {
  47. out += String.fromCharCode(bytes[i]);
  48. }
  49. var base64 = btoa(out);
  50. //Create FileCreationInformation object using the read file data
  51. createInfo = new SP.FileCreationInformation();
  52. createInfo.set_content(base64);
  53. createInfo.set_url(fileName);
  54. //Add the file to the list item
  55. attachmentFiles = attachmentFolder.get_files().add(createInfo);
  56. //Load client context and execute the batch
  57. clientContext.load(oList);
  58. clientContext.load(attachmentFiles);
  59. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
  60. }
  61. function QuerySuccess()
  62. {
  63. console.log("Attachment added successfuly ");
  64. }
  65. function QueryFailure(sender, args)
  66. {
  67. console.log('Request failed with error message - ' + args.get_message() + ' . Stack Trace - ' + args.get_stackTrace());
  68. }
  69. </script>
  70. </head>
  71. <body> <input id="getFile" type="file"/><br /> <input id="addFileButton" type="button" value="Upload" /> </body> <html>
Take the path of the file and save it.

SharePoint
Create a site page and add a CEWP to pull that file using the path saved earlier. Click "Apply" and save the site page.

SharePoint

Start uploading the attachments.

SharePoint

SharePoint
You can see the uploading of attachments happening.

SharePoint

Note

The above approach is successful on both, SharePoint Online and SharePoint On premise environments; as this is simple JavaScript client-side coding.

Keep changing the folder name and library name for your respective folder-wise documents whenever required.