上传html文件并使用python [英] uploading html files and using python

查看:438
本文介绍了上传html文件并使用python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的论坛,我尝试使用html上传文件:

 < form action =profiles.py方法= POST > 
< label class =labelOnefor =pics>更改您的个人资料图片< / label>
< input type =filename =uploadField/>

我的python函数使用该文件,在userprofiles下创建一个文件,并将数据写入它:

  file = open('../ data / accounts / userprofiles /'+ str(form ['name']。value) +'/'+'profilepics','w +')
file.write(str(form.getvalue('uploadField')))
file.close()

所以,如果我想让 burger.jpg 成为我的照片,我会上传它,python接受并使用 w + 创建一个名为 burger.jpg 的文件,然后将数据写入它(这应该是图像)。

然而由于某种原因,我没有形象。



接下来应该怎么办? 设置 enctype 您的表单到 multipart / form-data

 < form action =profiles.pymethod =postenctype =multipart / form-data> 


For my forum, I tried uploading a file with html using:

<form action="profiles.py" method="post">
<label class="labelOne" for="pics">Change your Profile pic </label>
<input type="file" name="uploadField" />

My python function takes that file, creates a file under userprofiles, and writes the data to it:

file = open('../data/accounts/userprofiles/'+str(form['name'].value)+'/'+'profilepics', 'w+')
file.write(str(form.getvalue('uploadField')))
file.close()

So, If I want burger.jpg to be my picture, I upload it, python takes that and creates a file with that name burger.jpg using w+ and then it writes the data to it(which should be the image).

However for some reason, I get no image.

What should I try next?

解决方案

Set the enctype of your form to multipart/form-data

<form action="profiles.py" method="post" enctype="multipart/form-data">

这篇关于上传html文件并使用python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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