How to check a uploaded file whether it is an image or other file?

I’m assuming that you’re running this in a servlet context. If it’s affordable to check the content type based on just the file extension, then use ServletContext#getMimeType() to get the mime type (content type). Just check if it starts with image/. String fileName = uploadedFile.getFileName(); String mimeType = getServletContext().getMimeType(fileName); if (mimeType.startsWith(“image/”)) { // It’s an …

Read more

How to read into memory the lines of a text file from an IFormFile in ASP.NET Core?

The abstraction for the IFormFile has an .OpenReadStream method. To prevent a ton of undesirable and potentially large allocations, we should read a single line at a time and build up our list from each line that we read. Additionally, we could encapsulate this logic in an extension method. The Index action ends up looking …

Read more

Get the filename of a fileupload in a document through JavaScript

In google chrome element.value return the name + the path, but a fake path. Thus, for my case I used the name attribute on the file like below : function getFileData(myFile){ var file = myFile.files[0]; var filename = file.name; } this is the call from the page : <input id=”ph1″ name=”photo” type=”file” class=”jq_req” onchange=”getFileData(this);”/>