在Windows上上传畸变图像的塔架 [英] Pylons Uploading Distorted Images on Windows

查看:159
本文介绍了在Windows上上传畸变图像的塔架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Pylons中创建一个Web应用程序,并正在进行图片上传操作。这是目前正在运行使用egg:粘贴#http在我的Windows机器,在基本的发展配置描述在pylons文档快速入门。



当我张贴图像到我的应用程序,然后将图片移动到网站根目录,然后在浏览器中上传图片,图片显示失真。这是我上传的雅虎GIF的时候得到的。标志,但是大多数文件根本不会在浏览器中显示出来,大概是因为损坏:

扭曲的雅虎标志http://www.freeimagehosting.net/uploads/d2c92aef00.png



这是我正在使用的基本代码(直接从主塔文档):

  os_path = os.path。 (config.images_dir,request.POST ['image'] .file)
save_file = open(os_path,'w')
shutil.copyfileobj(request.POST ['image']。file, save_file)
request.POST ['image']。file.close()
save_file.close()

request.POST ['image']是一个cgi.FieldStorage对象。我认为这可能是Windows线路结局的问题,但我不知道如何检查或纠正它。什么是导致我上传的图像扭曲/损坏?您可能只是缺少'b'(二进制)标志为了有效地将文件写成二进制文件:

$ $ $ $ $ c $ save_file = open(os_path,'wb')

但是我不明白为什么你需要在那里调用 shutil.copyfileobj ,为什么不这样做:



pre $ file_save_path = os.path.join(config.images_dir,request.POST ['image '] .filename)
file_contents = request.POST ['image'] .file.read()

#在这里插入完整性检查...

save_file = open(file_save_path,'wb')
save_file.write(file_contents)
save_file.close()

或者让最后三行更加pythonic(确保即使写入失败,文件句柄也被关闭):

打开(file_save_path,'wb')作为save_file 
save_file.wri te(file_contents)

您可能需要一个

  from __future__ import with_statements 

在您的顶部如果你是Python 2.6以下的文件。


I'm creating an web app in Pylons, and am working on an image upload action. This is currently running using egg:paste#http on my windows machine, in the basic development configuration described in the pylons documentation quickstart.

When I POST an image to my application, then move the image to the web root directory, then pull the uploaded image up in the browser, the image appears distorted. This is what I got when i uploaded a GIF of the Yahoo! logo, but most files don't show up in the browser at all, presumably because of corruption:

distorted yahoo logo http://www.freeimagehosting.net/uploads/d2c92aef00.png

This is the basic code I'm working with (straight out of the pylons documentation):

os_path = os.path.join(config.images_dir, request.POST['image'].filename)
save_file = open(os_path, 'w')
shutil.copyfileobj(request.POST['image'].file, save_file)
request.POST['image'].file.close()
save_file.close()

request.POST['image'] is a cgi.FieldStorage object. I thought this may be an issue with Windows line endings somehow, but I'm not sure how to check that or correct for it. What is causing my uploaded images to be distorted / corrupt?

解决方案

You are probably just missing the 'b' (binary) flag for effectively writing the file as binary:

save_file = open(os_path, 'wb')

But I don't see why you need the shutil.copyfileobj call in there, why not do something like this:

file_save_path = os.path.join(config.images_dir, request.POST['image'].filename)
file_contents = request.POST['image'].file.read()

# insert sanity checks here...

save_file = open(file_save_path, 'wb')
save_file.write(file_contents)
save_file.close()

Or make the last three lines a bit more pythonic (making sure the file handle gets closed even if the write fails):

with open(file_save_path, 'wb') as save_file:
    save_file.write(file_contents)

It's possible you need a

from __future__ import with_statements

at the top of your file if you're below Python 2.6.

这篇关于在Windows上上传畸变图像的塔架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆