This is a migrated thread and some comments may be shown as answers.

Upload file dimension

8 Answers 441 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 31 Jan 2019, 05:59 AM

Hello,

I have implemented the upload control in my angular application. I am restricting to only allow image and video file. my question is how do I check the image's dimension (width and height) before I move it to the server. is it possible to check the video (mp4)'s dimension as well? 

 

--Ben

8 Answers, 1 is accepted

Sort by
0
Dimiter Topalov
Telerik team
answered on 01 Feb 2019, 09:34 AM
Hi Ben,

You can create a new Image() object from the raw file, available in the select event handler, and access its naturalHeight and naturalWidth properties, e.g.:

https://stackblitz.com/edit/angular-zhb16m?file=app/upload.component.ts

Further details are available in multiple online resources, e.g.:

https://stackoverflow.com/a/13572240/7009923

https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

I hope this helps.

Regards,
Dimiter Topalov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Robert
Top achievements
Rank 1
answered on 20 Aug 2019, 06:18 AM

Hi,

but how can we do a selectEvent.preventDefault(); in the image.onload() if a non valid dimension is detected?

onload is asynchrounus and selectEventHandler() does not wait until the image is evaluated.

Kind regards
David

0
Dimiter Madjarov
Telerik team
answered on 21 Aug 2019, 07:48 AM
Hi David,


In this case I would suggest to set the autoUpload option of the Upload component to false, so the files are not immediately uploaded upon selection. When this option is set to false, two action buttons (Clear and Upload) will be displayed at the bottom. Those could also be temporary hidden via CSS while the file is being read using the FileReader and then shown again if the dimensions are valid. Another option for initiating the upload process would be to do it programatically using the uploadFiles method of the component

https://www.telerik.com/kendo-angular-ui/components/upload/api/UploadComponent/#toc-uploadfiles

Additionally, if the selected file should be removed from the list (in case it does not meet the requirements) I would recommend to use model binding with a collection of the selected files, as demonstrated here:

https://www.telerik.com/kendo-angular-ui/components/upload/model-binding/#toc-binding-of-file-collections

The component supports two-way binding, so when a file is programatically removed from the collection, it will also be removed from the files list (the collection instance should be changed in this case in order to trigger change detection).

Let me know if this information helps or I could provide further assistance.

Regards,
Dimiter Madjarov
Progress Telerik
Get quickly onboarded and successful with your Telerik and Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Robert
Top achievements
Rank 1
answered on 21 Aug 2019, 02:15 PM

Hi Dimiter,

yeah thanks, we've set autoupload to false, check within the (select) callback function, if its good, we call

this.kendoUploadComponent.uploadFiles();

directly.

If desired file dimensions not fit, we do:

this.kendoUploadComponent.fileList.clear();
selectEvent.preventDefault();
0
Dimiter Madjarov
Telerik team
answered on 21 Aug 2019, 03:25 PM
Hello Christian,


Yes, this solution is suitable. However keep in mind that fileList is an internal property. I would suggest to just reset the bound file collection to an empty array instead.

Regards,
Dimiter Madjarov
Progress Telerik
Get quickly onboarded and successful with your Telerik and Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Robert
Top achievements
Rank 1
answered on 22 Aug 2019, 08:48 AM

Hi Dimiter,

i have no access to the bound files in the selecthandler, as of api description:
When you use the ngModel directive, the model binding gets executed only after a successful file upload

Any other option to clear the internal fileList?

Kind regards
David

0
Dimiter Madjarov
Telerik team
answered on 22 Aug 2019, 01:43 PM
Hi Christian,


We released a new version 5.1.0 of the Upload component today that introduces a public clearFiles method. It will be suitable for the current or similar cases.

Regards,
Dimiter Madjarov
Progress Telerik
Get quickly onboarded and successful with your Telerik and Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Robert
Top achievements
Rank 1
answered on 23 Aug 2019, 02:36 PM
thanks, we use that now
Tags
Upload
Asked by
Ben
Top achievements
Rank 1
Answers by
Dimiter Topalov
Telerik team
Robert
Top achievements
Rank 1
Dimiter Madjarov
Telerik team
Share this question
or