squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.
MIT License
8.61k stars 581 forks source link

AttributeError: 'File' object has no attribute 'save' #151

Closed DiMiTriFrog closed 5 years ago

DiMiTriFrog commented 5 years ago

My server works perfectly with JSON and text requests, but when i try to receive files I have an error. Python Traceback ->

File "template.py", line 85, in uploadFile file.save(f) AttributeError: 'File' object has no attribute 'save'

The code I'm using : -> def uploadFile(request): file = request.files["data"] f = open("/Users/pewdepiebestthantseries/Documents/Japronto/uploader/" + request.files["data"].name, 'wb', 10000) file.save(f) print("All right") return request.Response(text="'Completed!'")

The content of data is an array that contains an image.

A simple request -> curl -i -X POST -H "Content-Type: multipart/form-data" -F "data=@/Users/pewdepiebestthantseries/Downloads/lasagna.png" http://IP:PORT/file

HEELP

squeaky-pl commented 5 years ago

I think you mean to call something like f.write(file.read()); f.close()

DiMiTriFrog commented 5 years ago

Hi! If I try this, the error code:

Traceback (most recent call last): File "template.py", line 85, in uploadFile file.write(f.read()) AttributeError: 'File' object has no attribute 'write'

python code ->

**def uploadFile(request):

print(request.files["data"])
file = request.files["data"]

f = open("/Users/pewdepiebestthantseries/Documents/Japronto/uploader/" + request.files["data"].name, 'wb', 10000)
file.write(f.read())
file.close()
print("All right")
return request.Response(text="Completed!")**

I try this too:

**def uploadFile(request):

print(request.files["data"])
file = request.files["data"]

f = open("/Users/pewdepiebestthantseries/Documents/Japronto/uploader/" + request.files["data"].name, 'wb', 10000)
f.write(file.read())
f.close()
print("All right")
return request.Response(text="'<h1>Completed!</h1>'")**

And the error code:

Traceback (most recent call last): File "template.py", line 85, in uploadFile f.write(file.read()) AttributeError: 'File' object has no attribute 'read'

Thanks!

squeaky-pl commented 5 years ago

I dont remember anymore, It looks like File is just a namedtuple

File = collections.namedtuple('File', ['type', 'body', 'name'])

can you try f.write(f.body); f.close()

DiMiTriFrog commented 5 years ago

Just Works! You're the fucking boss. Thanks !!

Post the code for help people to have an simple example to upload files with Japronto:

def uploadFile(request): file = request.files["data"] name_file = request.files["data"].name f = open("/Users/pewdepiebestthantseries/Documents/Japronto/uploader/" + name_file, 'wb') f.write(file.body) f.close() print("Upload new File: " + name_file) return request.Response(text="Completed")

app = Application() app.router.add_route('/file', uploadFile, method='POST') app.run(debug=True,port=8081)