Image uploader to use with file uploader from http://github.com/valums/file-uploader

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Created on Jan 7, 2012

@author: tgdn <tgdn45[AT]gmail[DOT]com>
"""


import json

def upload(request):
  """
  This snippet should be used with javascript file-uploader
  from Andrew Valums ( andrew(at)valums.com ) http://github.com/valums/file-uploader
  """
  
  # should be an ajax request
  if request.is_ajax():
    # Get image from raw data
    image = request.raw_post_data
    
    # The original filename
    name = request.GET.get('qqfile', 'some_name')
    
    # Where to upload the file
    url = '%s/uploads/%s' % (settings.MEDIA_ROOT, name)
    
    # Writing image to 'url' directory
    destination = open(url, 'wb+')
    destination.write(image)
    destination.close()
    
    """
    If you want to save the image in a database do like this:
    (according that 'Image' is the image model)
    *models.py*
    
    from django.db import models
    
    class Image(models.Model):
      image = models.ImageField(upload_to='uploads')
    
    ---
    
    And right after the image writing above:
    
    image = Image(image="uploads/%s" % name)
    image.save()
    
    """
    
    output = {
      'success': True
    }
    
    return HttpResponse(json.dumps(output))

More like this

  1. MultiFileWidget by Digitalxero 6 years, 2 months ago
  2. Fake File Uploads by rfk 5 years, 2 months ago
  3. Easy file upload handler by mattdw 5 years, 7 months ago
  4. File Mimetype Validator (Using python-magic) by BHSPitMonkey 2 weeks, 4 days ago
  5. Add a "remove file" field for Image- or FileFields by rodrigoc 5 years, 9 months ago

Comments

(Forgotten your password?)