我正在尝试使用IronPython中的以下调用上传文件
file={'uploadfile': ('data.csv', open("C:/Users/s/Desktop/data.csv",'rb'))}
print(file)
res = requests.post(url, files=file)
这适用于常规Python但在IronPython2.7.5中会抛出错误
"expected str, got bytes"
任何想法为什么?谢谢!
NeverMind,二手海报模块执行上传
from poster.encode import multipart_encode, MultipartParam
from poster.streaminghttp import register_openers
import urllib2
register_openers()
items=[]
items.append(MultipartParam.from_file("upfile", "c:/Users/s/Desktop/data.csv"))
datagen, headers = multipart_encode(items)
#print("".join(datagen))
req=urllib2.Request(url, datagen, headers)
res=urllib2.urlopen(req)
这似乎是海报没有正确关闭文件的问题,所以另一种方法
register_openers()
fileObj=open(dataFileLoc, 'rb')
fileData=MultipartParam(name=kfileUpload,filename=fileName,fileobj=fileObj)
items={kfileUpload: fileData}
datagen, headers = multipart_encode(items)
req=urllib2.Request(url, datagen, headers)
res=urllib2.urlopen(req)
fileObj.close()