mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
POST image data to View
- https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects - https://stackoverflow.com/questions/25390598/append-called-on-an-object-that-does-not-implement-interface-formdata#25390646
This commit is contained in:
parent
eec0fc34d2
commit
c88149b9aa
@ -110,7 +110,47 @@
|
||||
|
||||
console.log('dropped');
|
||||
|
||||
//$(this).removeClass('dragover');
|
||||
if (files.length > 0) {
|
||||
var file = files[0];
|
||||
|
||||
var formData = new FormData();
|
||||
|
||||
var token = getCookie('csrftoken');
|
||||
|
||||
formData.append('file', file);
|
||||
|
||||
$.ajax({
|
||||
beforeSend: function(xhr, settings) {
|
||||
xhr.setRequestHeader('X-CSRFToken', token);
|
||||
},
|
||||
url: "{% url 'part-image-upload' part.id %}",
|
||||
type: 'POST',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
success: function(data, status, xhr) {
|
||||
//location.reload();
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.log('Error uploading thumbnail: ' + status);
|
||||
console.log(error);
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
inventreeUpdate(
|
||||
"{% url 'part-image-upload' part.id %}",
|
||||
formData,
|
||||
{
|
||||
method: 'POST',
|
||||
dataType: 'json',
|
||||
}
|
||||
);
|
||||
*/
|
||||
|
||||
console.log('submitted');
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$("#show-qr-code").click(function() {
|
||||
|
@ -47,6 +47,10 @@ part_detail_urls = [
|
||||
|
||||
url(r'^qr_code/?', views.PartQRCode.as_view(), name='part-qr'),
|
||||
|
||||
# Drag-and-drop thumbnail upload
|
||||
url(r'^thumbnail-upload/?', views.UploadPartImage.as_view(), name='part-image-upload'),
|
||||
|
||||
# Normal thumbnail with form
|
||||
url(r'^thumbnail/?', views.PartImage.as_view(), name='part-image'),
|
||||
|
||||
# Any other URLs go to the part detail page
|
||||
|
@ -7,6 +7,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from django.http import JsonResponse
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic import DetailView, ListView
|
||||
from django.forms.models import model_to_dict
|
||||
@ -268,6 +269,31 @@ class PartImage(AjaxUpdateView):
|
||||
}
|
||||
|
||||
|
||||
class UploadPartImage(AjaxView):
|
||||
""" View for uploading a Part image """
|
||||
|
||||
model = Part
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
try:
|
||||
part = Part.objects.get(pk=kwargs.get('pk'))
|
||||
except Part.DoesNotExist:
|
||||
error_dict = {
|
||||
'error': 'Part not found',
|
||||
}
|
||||
return JsonResponse(error_dict, status=404)
|
||||
|
||||
print("Files:", request.FILES)
|
||||
uploaded_file = request.FILES['file']
|
||||
|
||||
response_dict = {
|
||||
'success': 'File was uploaded successfully',
|
||||
}
|
||||
|
||||
return JsonResponse(response_dict, status=200)
|
||||
|
||||
|
||||
|
||||
class PartEdit(AjaxUpdateView):
|
||||
""" View for editing Part object """
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user