Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload other file-types (e.g. XML) instead of images and read their file type #4

Open
JonathanSchmalhofer opened this issue May 29, 2022 · 6 comments

Comments

@JonathanSchmalhofer
Copy link

Hello and thanks for this nice addon,

I was able to make it run on text-based files like XML (see Link). However, I was not able to read out the file-type for such files. The returned variable seems empty.

As an example, I would like to handle uploads differently for *.JSON and *.XML files (but using the same Upload button, if possible).

Thanks!

@Pukkah
Copy link
Owner

Pukkah commented May 29, 2022

I believe you might be able to get file type like this:

...
var fileType = JavaScript.eval("_HTML5FileExchange.fileType", true)
var fileContent = JavaScript.eval("_HTML5FileExchange.result", true)
...

@JonathanSchmalhofer
Copy link
Author

Hm, tried that but the string returned seems to be empty (at least for XML-Files) - for PNG-images it returns "image/png" .

@Pukkah
Copy link
Owner

Pukkah commented May 29, 2022

I have not tried this with other files apart from images. I'll find some time this week to look into it, but meanwhile you can try to extract file extension from a file name.

@JonathanSchmalhofer
Copy link
Author

JonathanSchmalhofer commented May 29, 2022

Some more tests:

  • pdf returns "application/pdf"
  • ZIP returns "application/x-zip-compressed"
  • docx returns "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  • all ascii-based files return empty

@Pukkah : Yes, I will try that. Thanks for the support so far.

@Pukkah
Copy link
Owner

Pukkah commented May 29, 2022

Checking extension yourself or even validating the contents of the file might be a better solution when dealing with text files.
This is from JavaScript documentation:

Note: Based on the current implementation, browsers won't actually read the bytestream of a file to determine its media type. It is assumed based on the file extension; a PNG image file renamed to .txt would give "text/plain" and not "image/png". Moreover, file.type is generally reliable only for common file types like images, HTML documents, audio and video. Uncommon file extensions would return an empty string. Client configuration (for instance, the Windows Registry) may result in unexpected values even for common types. Developers are advised not to rely on this property as a sole validation scheme.

@FritzK-Hub
Copy link

I found a way to get text files to work.
You need to change this line in HTML5FileExchange.gd:
reader.readAsArrayBuffer(file);
to
reader.readAsText(file);

In input.setAttribute("accept", "image/png, image/jpeg, image/webp");
you can then use ".txt, .json, .custom" i.e. any UTF-8 encoded file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants