Windows 下 web.py上传图片乱码的解决办法
来源:互联网 发布:车载数据监控系统 编辑:程序博客网 时间:2024/06/05 08:05
今天在Windows平台下用web.py搭了个服务器,接收上传的图片并存储,参考的是官方cookbook里的示例代码:
def POST(self): x = web.input(myfile={}) filedir = '/path/where/you/want/to/save' # change this to the directory you want to store the file in. if 'myfile' in x: # to check if the file-object is created filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones. filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension) fout = open(filedir +'/'+ filename,'w') # creates the file where the uploaded file should be stored fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file. fout.close() # closes the file, upload complete. raise web.seeother('/upload')
然后在服务器端打开存储的图片,是大片大片的色块,完全不能用,一定是编码出问题了,网上搜了一下,参考豆瓣里这个帖子,把
fout = open(filedir +'/'+ filename,'w')替换成
fout = open(filedir +'/'+ filename,'wb')
问题就解决了。
原来,Windows下默认的写入是按照字符来写入的,而我们的图片是2进制的,所以在文件的打开方式参数里用‘b’来说明按照2进制来写入,问题就解决了。
0 0
- Windows 下 web.py上传图片乱码的解决办法
- Windows下安装Web.py快速指南
- Windows Phone7+Web Service的图片图片上传解决方案
- windows下git乱码解决办法
- domino环境下fckeditor编辑器无法上传图片的解决办法
- Windows下用CGI上传二进制文件损坏的解决办法
- web.py下session初始化的问题
- VS2008下web页面上传图片的自定义控件源码
- Windows 下以 nginx + fastcgi 运行 Django 或 web.py
- Windows下nginx+web.py+fastcgi服务搭建
- 在windows下如何快速搭建web.py开发框架
- 在windows下如何快速搭建web.py开发框架
- windows下Emacs中文乱码解决办法
- windows下git bash中文乱码解决办法
- windows下git bash中文乱码解决办法
- windows txt文件linux下乱码解决办法
- [图床神器]Windows下的图片上传工具MPic
- unzip解压windows上传的zip文件乱码问题的解决办法
- NSURLConnection实现HTTPS(SSL)链接请求
- 贫农的大数据之二(Kafka)
- myeclipse中java代码注释模板问题
- UIView动画(过渡效果)的学习笔记
- 如何快速查找电脑中的文件
- Windows 下 web.py上传图片乱码的解决办法
- LeetCode刷题笔录 Binary Tree Zigzag Level Order Traversal
- 打包和压缩及常见解压缩方法
- 多媒体之音频audio帖子汇总
- 2104腾讯暑期实习offer之路(技术类面经)
- 操作系统--信号量
- android ndk环境搭建总结
- 搬家到CSDN
- 关于NFC(近场通信)的三种响应模式(二)