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 of non-image files #330

Open
skylord123 opened this issue Sep 25, 2020 · 0 comments
Open

Upload of non-image files #330

skylord123 opened this issue Sep 25, 2020 · 0 comments

Comments

@skylord123
Copy link

Currently if you enable dropdown and drag-n-drop a non-image file it will try to render the file as an image. It would be nice to support any sort of file being uploaded instead of just images.

I was able to get this working by making these modifications to the drop zone success handler:

            options.dropZoneOptions.init = function() {
              var caretPos = 0;
              this.on('drop', function(e) {
                  caretPos = textarea.prop('selectionStart');
                });
              this.on('success', function(file, path) {
                console.log("success", file, path);
                  var url = window.URL || window.webkitURL;
                  var image = new Image();
                  image.onload = function() {
                    var text = textarea.val();
                      textarea.val(text.substring(0, caretPos) + '\n!['+file.name+'](' + path + ')\n' + text.substring(caretPos));
                    url.revokeObjectURL(image.src);
                  };
                  image.onerror = function() {
                    var text = textarea.val();
                      textarea.val(text.substring(0, caretPos) + '\n['+file.name+'](' + path + ')\n' + text.substring(caretPos));
                    url.revokeObjectURL(image.src);
                  };
                  image.src = url.createObjectURL(file);
              });
              this.on('error', function(file, error, xhr) {
                  console.log('Error:', error);
                });
            };

This also isn't dependent on the extension of the file because it tries to render the image using javascript and listens to the events. This works great for me and now non-image files are rendered as links instead.

If we decide to do a PR to merge this in we may want to change the image upload button to a file upload button.

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

1 participant