zoukankan      html  css  js  c++  java
  • ajax之---上传图片和预览

    views.py

    def upload_img(request):
    nid=str(uuid.uuid4())
    ret={'status':True,'data':None,'message':None}
    obj=request.FILES.get('k3')
    file_path=os.path.join('static',nid+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))

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .btn{
                display: inline-block;
                padding: 5px 10px;
                background-color: coral;
                color: white;
            }
        </style>
    </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/js/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>

  • 相关阅读:
    HTTP协议(三)之缓存
    第三方开源库MBProgressHUD的功能扩展
    CocoaPods pod install/pod update更新慢的问题
    项目总结Version 1.0(三)
    项目总结Version 1.0(二)
    iOS内存管理nonatomic,assign,copy,retain
    Mac升级到Yosemite后默认的php版本不支持imagetfftext函数问题解决
    3333333333333333
    11111111111111111111
    ssssssssssssssss
  • 原文地址:https://www.cnblogs.com/jinxf/p/9164068.html
Copyright © 2011-2022 走看看