使用python,cgi.FieldStorage()上传图片为空

时间:2015-04-30 02:53:16

标签: ajax python-3.x

我正在尝试使用ajax调用上传图像到python脚本但是cgi.FieldStorage()是空的,请建议我做错了什么,感谢任何帮助。

HTML代码

<form id="imgform" method="post" enctype="multipart/form-data">
    <div style="display: inline-block;">
        <img style="width:150px;" id="bookimg" src="#" alt="" name="bookimg" />
        <input type="file" name="imginp" id="imgInp"/>
    </div>
    <div style="display: inline-block;">
        <input id="upload" type="button" value="Upload" />
    </div>
    <img src="images/loading.gif" id="busy_wait" alt="busy wait icon" />  
</form>

JS代码

$("#upload").on('click', function(){
        var imgformdata = $("#imgform").serialize();
        $.post("python/processimage.py", imgformdata, function(response){
                response=$.trim(response);
                if(response == "invalidsession"){
                    window.location = 'logout.php';
                }else if(response == "ok"){
                    $(".imgdiag").html(response);
                }else{
                    $('#imguptext').text(response);
                }
            });
    });

Python代码

#!C:\....\python.exe
import cgi, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()

# A nested FieldStorage instance holds the file
if len(form) > 0:
    fileitem = form["imginp"]

from PIL import Image, ImageFilter
print("Content-Type: text/html")
print()
print("<html><head><title>Python</title></head><body>")
print("<h5>"+str(len(form))+"</h5>")

1 个答案:

答案 0 :(得分:0)

我更改了javascript,它运行正常 -

var flatten = function(input) {
    return eval("[" + JSON.stringify(input).
    replace(/\[/g,"").replace(/\]/g,"") + "]");
}