English 中文(简体)
卸载Django
原标题:FileUpload with Django

http://valums.com/ajax-upload/“rel=“noretinger”>ajax-upload 穿透宇宙航空研究开发机构档案的密码。 问题Im交叉起来,是卷宗号,在提交后显示。

The frontend code is pretty basic:

<div id="image_uploader">Upload More Images</div>
<script type="text/javascript" charset="utf-8">
    function createUploader(){            
        var uploader = new qq.FileUploader({
            element: document.getElementById( image_uploader ),
            action:  /add/image/1 ,
            debug: true,
            onSubmit : function () {
                progress.show();
            },
            onComplete : function () {
                progress.hide();
            },
            onCancel : function () {
                progress.hide();
            },
        });           
    };

    createUploader();
</script>

后附法(目前仍在进行之中)也基本如下:

def add_image(request, id):
    print request
    if request.FILES:
        return HttpResponse("{success:true}")
    else:
        return HttpResponse("{success:false, message: Unable to find FILES}")
最佳回答

对我来说,使用;Alex Kuhl;request.GET[qqfile ]有文档名称和request.read(在Django 1.3中)填交还了数据。

请求。 只有在我尚未出现的情况下才使用方济会。 我用Ajax-upload直接与照片交谈,而我的代码认为:

def save_upload( uploaded, filename, raw_data ):
    """
    raw_data: if True, upfile is a HttpRequest object with raw post data
    as the file, rather than a Django UploadedFile from request.FILES
    """
    try:
        filename = os.path.normpath(os.path.join(IMAGE_UPLOAD_PATH, filename))
        with BufferedWriter( FileIO( filename, "wb" ) ) as dest:
            # if the "advanced" upload, read directly from the HTTP request
            # with the Django 1.3 functionality
            if raw_data:
                (dirName, fileName) = os.path.split(filename)
                (fileBaseName, fileExtension)=os.path.splitext(fileName)
                #
                # right here, if fileBaseName is less than n characters, might want to slap on a date just for fun
                #
                try:
                    i_can_has_p = Photo.objects.get(title=fileBaseName)
                    title = fileBaseName + "_" + str(datetime.datetime.now().strftime("%Y%m%dT%H%M%S"))
                except Photo.DoesNotExist:
                    title = fileBaseName
                title_slug = slugify(title)
                p = Photo(title=title, title_slug=title_slug)
                p.image.save(filename,ContentFile(uploaded.read()))
            # if not raw, it was a form upload so read in the normal Django chunks fashion
            else:
                # TODO: figure out when this gets called, make it work to save into a Photo like above
                for c in uploaded.chunks( ):
                    dest.write( c )
    except IOError:
        # could not open the file most likely
        return False
    return True

def ajax_upload( request ):
  if request.method == "POST":
      # AJAX Upload will pass the filename in the querystring if it is the "advanced" ajax upload
      if request.is_ajax( ):
          # the file is stored raw in the request
          upload = request
          is_raw = True
          try:
              filename = request.GET[  qqfile  ]
          except KeyError:
              return HttpResponseBadRequest( "AJAX request not valid" )
      # not an ajax upload, so it was the "basic" iframe version with submission via form
      else:
          is_raw = False
          if len( request.FILES ) == 1:
              # FILES is a dictionary in Django but Ajax Upload gives the uploaded file an
              # ID based on a random number, so it cannot be guessed here in the code.
              # Rather than editing Ajax Upload to pass the ID in the querystring, note that
              # each upload is a separate request so FILES should only have one entry.
              # Thus, we can just grab the first (and only) value in the dict.
              upload = request.FILES.values( )[ 0 ]
          else:
              raise Http404( "Bad Upload" )
          filename = upload.name

  # save the file
  success = save_upload( upload, filename, is_raw )

  # let Ajax Upload know whether we saved it or not
  ret_json = {  success : success, }
  return HttpResponse( json.dumps( ret_json ) )

就我而言,ajax_upload is the function calls by ajax s action: 参数

问题回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...