Introduction
In this article, we will discuss about how to implement Kendo upload fuctionality and how to retreive the Excel data in the form of Json.
Kendo UI Upload Widget
Kendo Upload has many features in built. Below is the kendo upload with the restriction to upload only xlsx files. Likewise we can add the file extensions separated by commas.
- <input type="file" id="fileUpload" name="fileUpload" />
- <script type="text/javascript">
- $("#fileUpload").kendoUpload({
- validation: {
- allowedExtensions: [".xlsx"],
- maxFileSize: 900000,
- },
- multiple: false,
- select: attachClickHandler
- })
- function attachClickHandler(evt) {
- var selectedFile = evt.files[0];
-
-
-
- }
- </script>
Features in Kendo Upload
- Kendo Upload has features to browse the file directory which is the default one by selecting the file or we can drag and drop the file.
- Async Upload for multiple files.
- Localization- Set Custom strings and messages for the upload process.
- Template- Custom text whatever we give , it will replace in the place where the default file name is displayed.
- Valiation - to restrict extensions, minimum file size, maximum file size.
- multiple: false - we have the restriction to whether user can select multipile files for upload. By default it will be true. If we set to false, user can select only one file.
How to convert the Excel data to Json
Step 1
Please add the below plugin which is used for the Excel conversion to json.
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.12/xlsx.full.min.js"></script>
Step 2
Please add upload control in your html file either Kendo Upload/ jquery file Upload
- <label for="myfile">Select a file:</label>
- <input type="file" id="myfile" name="myfile"><br><br>
- <p id="jsonText"></p>
Step 3
-
- $("#myfile").change(function(evt){
- var file = evt.target.files[0]
- fnConvertExcelToJSON(file)
- })
- function fnConvertExcelToJSON(file){
- var reader = new FileReader()
- reader.onload = function(event) {
- var data = event.target.result
- var workbook = XLSX.read(data, {
- type: 'binary'
- });
- var json_object
- workbook.SheetNames.forEach(function(sheetName) {
- var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName])
- json_object = JSON.stringify(XL_row_object)
- var jsonOutput = JSON.stringify(JSON.parse(json_object), undefined, 4);
- $('#jsonText').html(jsonOutput)
- })
- }
- reader.onerror = function(event) {
- console.error("File could not be read! Code " + event.target.error.code)
- }
- reader.readAsBinaryString(file);
- }
-
The above code will give you the excel value in JSON array. Initially the uploaded file will be rendered in upload change event. Then the rendered file data will initialized to XLSX read. A Excel workbook can have multiple sheets, so loop through the sheet one by one. Using the XLSX.utils.sheet_to_row_object_array method, the excel data will be converted.
Output Screenshots
Excel data
Upload Excel
Json Output
Summary
In this article, I discussed how to retreive theExcel data in the form of Json from the uploaded files.