0

I am making a file uploader using html tags. My table consists of id(Primary Key), Name varchar(50), ContentType(which shows me the extension of the file) and Data varbinary(Max) which will store the contents of the file. Below is my html:

     <tr>
     <td class="label" style="width:15%">
     Upload File
     </td>
     <td class="description" >
     <input type="file" id="FileUpload1" class="largeTextField" multiple="multiple"   
     style="width:260px;"/>
     <input type="button" id="btnUpload" value="Upload"  onclick="UploadFile()" />
     </td>
     <div class="validator" id="txtUploadFileVld" style="display: none">
     *</div>
     </tr>

and javascript function UploadFile():

    function UploadFile() {

    var filename = $('input[type=file]').val().split('\\').pop();
    var filePath = $('#FileUpload1').val();
    var contenttype = $("#FileUpload1").val().split('.').pop();
    var AJAX = new AJAXsupport();
    AJAX.resetVar();
    AJAX.addData('id', id);
    AJAX.addData('Name', $('#FileUpload1').val().split('\\').pop());
    AJAX.addData('ContentType', $('#FileUpload1').val().split('.').pop());

    AJAX.addData('CLDone', 'UploadFile');
    var sucSave = function () {
        alert(AJAX.getMessage())



        }
    customSave(AJAX, sucSave);
     }

I can access the filename and contenttype through my function. They are also correctly being saved in the database. But I want to get the contents of the file as well and save it in database. Can someone edit my code above to achieve my task? I don't know how can I do so.

Unknown
  • 145
  • 2
  • 11

1 Answers1

0

Access content of a text file

Here's one way you can access the content of a text file.

Reference: How to read a local text file

When trying to do this with Node, you would need to install xmlhttprequest package since Node doesn't natively support browser's XHR API, and then just require it:

const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
function readTextFile(file) {
    var rawFile = new XMLHttpRequest();
    rawFile.open('GET', file, false);
    rawFile.onreadystatechange = function () {
        if(rawFile.readyState === 4) {
            if(rawFile.status === 200 || rawFile.status == 0) {
                var allText = rawFile.responseText;
                console.log(allText);
            }
        }
    }
    rawFile.send(null);
}

readTextFile('file://C:/path/to/your-file/test-file.txt');

Hope this helps.

Community
  • 1
  • 1
  • I do not want to make a separate function for this. I want to modify my code somehow to achieve my task. – Unknown Feb 08 '20 at 14:58