js 模拟ajax方式提交数据

时间:2023-05-21 13:33:13

html页面

<script>
function LocaluploadCallback(msg) {
    document.getElementById("f_localupload").outerHTML = document.getElementById; //清空
    alert(msg);
}
</script>

<form id="formlocalupload" action="/album/upload/" enctype="multipart/form-data" method="post" target="hidden_frame">
    <input type="file" name="f_localupload" id="f_localupload" accept="image/jpg,image/jpeg,image/gif,image/png,image/bmp" />
    <input type="submit" name="submit" value="提交" />
</form>

<iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>

后台程序

def upload(request):
    try:
       user = request.user
        #file = request.FILES.get('f_localupload')
        file_obj = request.FILES.values()[0]
        return HttpResponse("<script>parent.LocaluploadCallback(‘ok’) </script>")
     except Exception, e:
        log.error(e)
        return HttpResponse("<script>parent.LocaluploadCallback('error') </script>")