Example #1
0
def upload_image(request):
    """上传文章图片, 专门针对kindeditor 的图片上传"""
    if request.method == "POST" and "imgFile" in request.FILES:
        try:
            file = request.FILES["imgFile"]
            request.FILES["file"] = file
            from article.forms import AttachmentForm
            form = AttachmentForm(request.POST, request.FILES)
            if form.is_valid():
                _form = form.save(commit=False)
                _form.user = request.user
                _form.save()

                article_pk = request.POST.get("article_pk", None)
                try:
                    article = Article.objects.get(pk=article_pk)
                except Article.DoesNotExist,e:
                    article = None

                if article:
                    _form.article = article
                    _form.save()

                print _form.file.url
                res  = json.dumps({
                    "error": 0,
                    "url": _form.file.url,
                })
            else:
                res  = json.dumps({
                    "error": 1,
                })
            print res
            return HttpResponse(res)
Example #2
0
def upload_attachment(request):
    """上传文章附件"""
    if request.method == "POST" and "file" in request.FILES:
        try:
            file = request.FILES["file"]
            from article.forms import AttachmentForm
            form = AttachmentForm(request.POST, request.FILES)
            if form.is_valid():
                _form = form.save(commit=False)
                _form.user = request.user
                _form.save()

                article_pk = request.POST.get("article_pk", None)
                try:
                    article = Article.objects.get(pk=article_pk)
                except Article.DoesNotExist,e:
                    article = None

                if article:
                    _form.article = article
                    _form.save()

                print _form.file.url
                res  = json.dumps({
                    "success": True,
                    "data": {
                        "uploaded_file_url": request.build_absolute_uri(_form.file.url),
                        "attachment_pk": _form.pk
                    }
                })
            else:
                res  = json.dumps({
                    "success": False,
                })
            return HttpResponse(res)