【发布时间】:2018-06-13 20:51:52
【问题描述】:
我正在使用 Python-flask 应用程序进行图像处理,但是,当我尝试通过 request.args.get 以 python 方法访问图像时,图像压缩在 JavaScript 中完成,然后在 python 烧瓶后端完成上传('image'),它给出 None 详细信息如下
var img = $('#img-upload')[0];
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#img-upload').attr('src', e.target.result);
img.onload = function() {
alert("Image is loaded");
var MAX_WIDTH = 100;
var MAX_HEIGHT = 100;
var width = img.width;
var height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
var canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
canvas.getContext("2d").drawImage(this, 0, 0, width, height);
var newImageData = canvas.toDataURL('image/png', 30/100);
var result_image_obj = new Image();
result_image_obj.src = newImageData;
console.log(result_image_obj.src);
console.log("Starting Upload...");
if (result_image_obj.src == "") {
alert("You must load an image and compress it first!");
return false;
}
var callback= function(response){
console.log("image uploaded successfully! :)");
console.log(response);
}
$.ajax({
url:"/users/api/v1/uploadimage",
type:'POST',
data:{'image':result_image_obj},
cache:false,
processData:false,
contentType:false,
error:function(){
console.log("upload error")
},
success:function(data){
console.log(data)
console.log("upload success")
}
})
console.log("Completed Upload...");
}
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" action="/updateuser" method="POST" enctype="multipart/form-data">
<input type='file' id="imgInp"/>
<img id="img-upload" name="img-upload" src="#"/>
</form>
@app.route('/users/api/v1/uploadimage',methods=['GET','POST'])
def uploadimage():
print "In uploadimage()"
try:
file = request.args.get('image')
print "Filename",file
except Exception as e:
print str(e)
return "True";
【问题讨论】:
-
感谢您的编辑和帮助。我没有在 python 中得到我在 ajax 调用中传递的图像参数。我做错了什么?请建议。
-
在 flask.pocoo.org/docs/0.12/patterns/fileuploads 链接中,我们接受 标签的参数,但在我的情况下,我需要压缩图像对象。我怎样才能在 python 中得到这个?
-
或者另一方面,我怎样才能将此压缩对象设置为其他对象,以便我可以在 python 中访问它?请建议。
-
等一下,我有一个建议!您将不得不在您的服务器中保存图像对吗?@VinayakMahajan
标签: javascript python flask image-upload