美文网首页ajax总结
伪AJAX例子 - 数据,文件上传(iframe)

伪AJAX例子 - 数据,文件上传(iframe)

作者: 赖三石 | 来源:发表于2017-07-28 17:32 被阅读0次

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.index, name='index'),
    url(r'^iframe_test/', views.iframe_test, name='index'),
]

template-index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <form action="/iframe_test/" method="post" enctype="multipart/form-data" target="ifr">
        <iframe id="ii" name="ifr"></iframe>
        <input type="text" name="u">
        <input type="text" name="p">
        <input type="file" name="fff">
        <input type="submit" value="submit" onclick="click001()">
    </form>
</body>
<script src="/static/jquery.js"></script>
<script>
    function click001(){
        $('#ii').load(function () {
            var text = $('#ii').contents().find('body').text();
            console.log(JSON.parse(text));
        })
    }
</script>
</html>

views.py

from django.shortcuts import render,HttpResponse

# Create your views here.

def index(request):
    return render(request, 'index.html')


def itest(request):
    u = request.POST.get('u')
    p = request.POST.get('p')
    fileobj = request.FILES.get('fff')
    # print fileobj.chunks(), fileobj.name

    import os
    file_path = os.path.join('static', fileobj.name)
    with open(file_path, 'wb') as f:
        for i in fileobj.chunks():
            f.write(i)

    res = {'u': u, 'p': p}
    import json
    return HttpResponse(json.dumps(res))

相关文章

网友评论

    本文标题:伪AJAX例子 - 数据,文件上传(iframe)

    本文链接:https://www.haomeiwen.com/subject/shsflxtx.html