python2.7版本,使用Pycharm運(yùn)行python項(xiàng)目的gui.py文件時(shí)提示app.py的第17行代碼(也就是filename = i.file.filename)出錯(cuò),錯(cuò)誤信息:AttributeError: 'dict' object has no attribute 'filename'
但是代碼已經(jīng)對(duì)file進(jìn)行了初始化了:
i = web.input(file = {}) #接收數(shù)據(jù)
filename = i.file.filename #獲取文件名
file = i.file.file.read() #獲取文件
請(qǐng)問(wèn)為啥還是出現(xiàn)這個(gè)錯(cuò)誤?
html代碼為:
<!DOCTYPE html>
<html>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="post">
</form>
</body>
</html>
gui.py代碼為:
# -*- coding:utf-8 -*-
from Tkinter import *
import tkFileDialog
import urllib2
import sys
import win32clipboard as w
import win32con
import win32api
import tkMessageBox
# reload(sys)
# sys.setdefaultencoding("utf-8")
def setText(text):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_TEXT,text)
w.CloseClipboard()
def upload():
filename = tkFileDialog.askopenfilename(title="選擇文件")#選擇文件,返回文件名
files = open(filename,'rb').read()
data ='''------WebKitFormBoundaryDLanRACtPqUEBKKs
Content-Disposition: form-data; name="file"; filename="%s"
Content-Type: application/octet-stream
[file]
------WebKitFormBoundaryDLanRACtPqUEBKKs--'''%filename.split('/')[-1]
data = bytes(data)
data = data.replace(bytes('[file]'),files)
req = urllib2.Request('http://127.0.0.1:8080/upload',data)
req.add_header('Content-Type','multipart/form-data; boundary=----WebKitFormBoundaryPZsy5bHyBCEivf53')
html = urllib2.urlopen(req).read()
print html
ent.delete(0,END)
ent.insert(0,html)
def download():
files = urllib2.urlopen(ent.get()).read()
filename = tkFileDialog.asksaveasfilename()
with open(filename,'wb') as fn:
fn.write(files)
def copy():
setText(ent.get())
tkMessageBox.showinfo('ok','url已復(fù)制')
root = Tk()#創(chuàng)建窗口
root.title("文件分享系統(tǒng)")#修改窗口名
root.geometry("300x130+500+300")#修改窗口大小和位置
ent = Entry(root,width = 50)#輸入框
ent.grid()#顯示控件
btn_upload = Button(root,text=" Upload ",command=upload)
btn_upload.grid()
btn_download = Button(root,text="Download",command=download)
btn_download.grid()
btn_copy = Button(root,text=" Copy url ",command=copy)
btn_copy.grid()
mainloop()#顯示窗口
app.py代碼為:
# -*- coding:utf-8 -*-
import web
urls = (
#'/my','My',#瀏覽器訪問(wèn)http://127.0.0.1:8080/my時(shí),就會(huì)調(diào)用My這個(gè)類的GET方法
'/','Index',
'/upload','Upload',
)#路由
render = web.template.render('templates')
class Index:
def GET(self):
return render.index()
class Upload:
def POST(self):
i = web.input(file = {}) #接收數(shù)據(jù)
filename = i.file.filename #獲取文件名
file = i.file.file.read() #獲取文件
with open('static/%s' %filename,'wb') as fn:
fn.write(file)
return 'http://127.0.0.1:8080/static/%s' %filename
app = web.application(urls,globals())
if __name__== '__main__':#入口函數(shù)判斷,本文件調(diào)用時(shí),__name__== '__main__',其他文件調(diào)用時(shí),__name__==文件名
app.run()
學(xué)習(xí)是最好的投資!
The file upload was not successful.
The problem lies in the upload. The value behind this Boudary is not fixed. urllib2
沒(méi)有處理MIME的功能,要配合其它庫(kù)比如poster
Use.
Recommend you to use requests
, the best http library in Python.
import requests
data = {'file': open('a.out','rb')} # 這里a.out是你要上傳的文件名
requests.post('http://127.0.0.1:8080/upload',files=data)