0

I want to check the extension of uploaded file. It is working fine but what if someone change the extension of uploading file. Like if someone change the .pdf extension to .png or .jpeg , then how to check this using vue.js. I have tried active storage validator gem but then there are many changes in my app. I want to write lesser code. If anyone have efficient approach, please share.

test user
  • 27
  • 6
  • 1
    there's no native way in Vue to upload a file so it really depends on the library or strategy you're using to upload. surely whatever you're using you can allow you to check the file object for the extension before sending it in a request to your backend. without any actual details about your frontend code all we can do is guess. please realize, [we know nothing about your app](https://stackoverflow.com/help/how-to-ask). – yoduh Jan 06 '23 at 18:23
  • 1
    I think you should revise the title to "How to check the [file type] of an uploaded file regardless of the extension". As you've identified using the extension alone tells you nothing about the contents of the file, checking the MIME type would be a better solution but I am unaware of a frontend library that would provide a deterministic MIME type. Even the [File API](https://developer.mozilla.org/en-US/docs/Web/API/File/type) relies on the assumption that the extension is correct. This [SO Post](https://stackoverflow.com/a/29672957/1978251) provides a fairly comprehensive explanation. – engineersmnky Jan 06 '23 at 20:27
  • 1
    @engineersmnky one thing to keep in mind is that MIME types are almost as easy to spoof as a file extensions and you shouldn't trust the MIME types of user provided files. They give a hint of whats in the file at best. – max Jan 07 '23 at 14:11

0 Answers0