python文件上传的三种方式

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

def upload_file(request):
    username = request.POST.get('username')
    fafafa = request.FILES.get('fafafa')

    with open(fafafa.name, 'wb') as f:
        for item in fafafa.chunks():
            f.write(item)
    print(username)
    ret = {'code': '123456', 'data': 'hahahaha'}
    import json
    return HttpResponse(json.dumps(ret))
views.py

1.xmlHttpResquest

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
    <style>
        .file{
            width: 100px;
            height: 50px;
            position: absolute;
            z-index: 100;
            opacity: 0;
        }

        .upload{
            width: 100px;
            height: 50px;
            position: absolute;
            z-index: 90;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
            background-color: blue;
            text-align: center;
            line-height: 50px;
            border-radius: 30px;
            color: white;
        }
    </style>

<body>
    <div style="position: relative;height: 50px;width: 100px;">
        <input class="file" type="file" id="fafafa">
        <a class="upload">上传</a>
    </div>
    <input type="button" value="xhr提交" onclick="xhrSubmit()">

    <script src="/static/jquery-1.12.4.js"></script>
    <script>
        function xhrSubmit() {
            var file_obj = document.getElementById('fafafa').files[0];
            var fd = new FormData();
            fd.append('username','root')
            fd.append('fafafa',file_obj)

            var xhr = new XMLHttpRequest();
            xhr.open('POST', '/upload_file/',true);
            <!--onreadystatechange可以监测xhr的状态变化-->
            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4){
                    // 接收完毕
                    var obj = JSON.parse(xhr.responseText);
                    console.log(obj);
                }
            };
            xhr.send(fd);
        }
    </script>
</body>
</html>
View Code

2.jQuery

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
    <style>
        .file{
            width: 100px;
            height: 50px;
            position: absolute;
            z-index: 100;
            opacity: 0;
        }

        .upload{
            width: 100px;
            height: 50px;
            position: absolute;
            z-index: 90;
            top: 0;
            bottom: 0;
            right: 0;
            left: 0;
            background-color: blue;
            text-align: center;
            line-height: 50px;
            border-radius: 30px;
            color: white;
        }
    </style>

<body>
    <div style="position: relative;height: 50px;width: 100px;">
        <input class="file" type="file" id="fafafa">
        <a class="upload">上传</a>
    </div>
    <input type="button" value="jQuery提交" onclick="jqSubmit()">

    <script src="/static/jquery-1.12.4.js"></script>
    <script>
        function jqSubmit() {
            {#var file_obj = $('fafafa').files[0];#}
            var file_obj = document.getElementById('fafafa').files[0];
            var fd = new FormData();
            fd.append('username','root');
            fd.append('fafafa',file_obj);

            $.ajax({
                url: '/upload_file/',
                type: 'post',
                data: fd,
                processData: false, // tell jQuery not to process the data
                contentType: false, // tell jQuery not to set contentType
                success:function (arg,a1,a2) {
                    console.log(arg);
                    console.log(a1);
                    console.log(a2);
                }
            })
        }
    </script>
</body>
</html>
View Code

3.iframe

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/upload_file/" method="POST" enctype="multipart/form-data" target="ifm1">
        <!--这里添加iframe是由于需要有一个html接收"/upload_file/"方法返回的参数,一般为HttpResponse,所以这里使用iframe接收,
        iframe一般设置为"display:none"将其隐藏-->
        <iframe id='ifm1' name="ifm1"></iframe>
        <input type="file" name="fafafa">
        <input type="submit" onclick="iframeSubmit()" value="iframe提交">
    </form>

    <script src="/static/jquery-1.12.4.js"></script>
    <script>
        function iframeSubmit() {
            $('#ifm1').load(function () {
                var text = $('#ifm1').contents().find('body').text();
                var obj = JSON.parse(text)
                console.log(obj)
            })
        }
    </script>
</body>
</html>
View Code

猜你喜欢

转载自www.cnblogs.com/ttyypjt/p/10184469.html