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

How i does custom html choose image upload? #36

Closed
agusmakmun opened this issue Dec 12, 2016 · 3 comments
Closed

How i does custom html choose image upload? #36

agusmakmun opened this issue Dec 12, 2016 · 3 comments

Comments

@agusmakmun
Copy link

agusmakmun commented Dec 12, 2016

This markdownx use drag & drop to upload the image. but how i can implement it with html choose image? example:

{% extends 'base.html' %}

{% block content %}
    <form role="form" method="POST" action="">{% csrf_token %}
        {{ form.as_p }}
        <input type="file" class="upload-img" accept="image/*" placeholder="Choose image or drag & drop...">
    </form>
    {{ form.media }}

    <script>
        $('.upload-img').on('change', function() {
            //what i can do here?
        });
    </script>
{% endblock %}

Another problem, how i can implement it at django admin page?

@agusmakmun agusmakmun changed the title How i does custom html choose image How i does custom html choose image? Dec 12, 2016
@agusmakmun agusmakmun changed the title How i does custom html choose image? How i does custom html choose image upload? Dec 12, 2016
@adi-
Copy link
Member

adi- commented Dec 13, 2016

There is no easy way to do it "quick", I think. You could change JS script a bit to be able to send images. This should be a simple change.

@agusmakmun
Copy link
Author

How i can do it? can u give me an example?

@agusmakmun
Copy link
Author

agusmakmun commented Dec 19, 2016

Finally i got it.. 😄

Inside file of dummy.html

<script src="/static/suit/js/jquery-1.8.3.min.js"></script>

<form enctype="multipart/form-data" method="POST" action=".">{% csrf_token %}
  {{ form.image }}
</form>

<script>
    // CSRF code
    function getCookie(name) {
        var cookieValue = null;
        var i = 0;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (i; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    // Uploading the image if on change.
    $('input[name=image]').on('change', function(evt) {
      evt.preventDefault();
      var formData = new FormData($('form').get(0));
      formData.append("csrfmiddlewaretoken", getCookie('csrftoken'));

      $.ajax({
          url: '/markdownx/upload/',
          type: 'POST',
          data: formData,
          async: true,
          cache: false,
          contentType: false,
          enctype: 'multipart/form-data',
          processData: false,
          beforeSend: function() {
              console.log("uploading...");
          },
          success: function (response) {
              console.log(response['image_code']);
          },
          error: function(response) {
              console.log("error", response);
          }
      });
      return false;
    });
</script>

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

2 participants