基于iframe 文件预览

html

views



代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<iframe style="display: none" id="iframe1" name="ifra1"></iframe>
    <form id="fm1" action="upload_img.html" method="POST" enctype="multipart/form-data" target="ifra1">
        <input type="file" name="k3" onchange="uploadFile();" />
    </form>
    <h3>预览</h3>
    <div id="preview">
    </div>

<script src="/static/jquery-3.1.1.js"></script>
<script>

      function uploadFile() {
            document.getElementById('iframe1').onload = reloadIframe1;
            document.getElementById('fm1').submit();
        }
        function reloadIframe1() {
            var content = this.contentWindow.document.body.innerHTML;
            var obj = JSON.parse(content);

            var tag = document.createElement('img');
            tag.src = obj.data;
            $('#preview').empty().append(tag);
        }
</script>
</body>
</html>
import json
import os
def ajax2(request):
    return render(request,'ajax2.html')

def upload_img(request):
    ret = {'status':True,'data':None,'message':None}
    obj = request.FILES.get('k3')
    file_path = os.path.join('static',obj.name)
    f = open(file_path,'wb')
    for line in obj.chunks():
        f.write(line)
    f.close()
    ret['data'] = file_path
    return HttpResponse(json.dumps(ret))










扫描二维码关注公众号,回复: 1780125 查看本文章










猜你喜欢

转载自blog.csdn.net/weixin_42100915/article/details/80784945