Вопрос

I'm using cherrypy to save a WAV file from the browser to local disk. Incoming:

{'audio_file': [<cherrypy._cpreqbody.Part object at 0x7fd95a409a90>, <cherrypy._cpreqbody.Part object at 0x7fd95a178190>], 'user_data': u'{"id":"1255733204",'audio_length': [u'10.03102', u'22.012517', u'22.012517']}

I get this error:

try:
        f = open('/audiostory/'+filename,'wb')
        logging.debug( '[SAVEAUDIO] tried to write all of audio_file input at once' )
        f.write(kw.get('audio_file'))
        f.close()
        logging.debug( ('saved media file %s to /audiostory/' % f.name) )
except Exception as e:
        logging.debug( ('saved media NOT saved %s because %s' % (f.name,str(e))) )

"Must be convertible to a buffer, not Part."

So how do I handle this kind of data, that cherrypy is converting to a '.Part' but it should be raw wav data? Am I missing a header or something?

UPDATED

Jason - you'll see I purposely did not send any header or whatnot because I wanted to see what cherrypy would deliver raw. Here is the cherrypy site.py file:

@cherrypy.expose
def saveaudio(self, *args, **kw):
    import audiosave
    return audiosave.raw_audio(kw.get('audio_file'))

and the function within audiosave:

def raw_audio(raw):
""" is fed a raw stream of data (I think) and saves it to disk, with logging. """
import json
import logging        
LOG_FILENAME = 'error.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug( '[SAVEAUDIO(raw)]'+str(raw) )
filename = assign_filename()
try:
    #SAVE FILE TO DISK /s/audiostory/
    f = open('/home/djotjog/webapps/s/audiostory/'+filename,'wb')
    logging.debug( '[SAVEAUDIO] tried to write all of audio_file input at once' )
    f.write(raw)
    f.close()
    logging.debug( ('media SAVED %s' % f.name) )
except Exception as e:
    logging.debug( ('media NOT saved %s because %s' % (f.name,str(e))) )
    return json.dumps({"result":"414","message":"ERROR: Error saving Media file "+f.name})
return raw

I also tried f.write(raw.file.read()) but same error occurs.

Это было полезно?

Решение

This complete example demonstrates a CherryPy server that serves the HTML form and then receives the file uploaded from the client.

html = """
<html>
<body>
<form enctype="multipart/form-data" method="post" action="audio">
    <input type="file" name="recording">
    <button type="submit">Upload</button>
</form>
</body></html>
"""

import cherrypy

class Server:
    @cherrypy.expose
    def index(self):
        return html

    @cherrypy.expose
    def audio(self, recording):
        with open('out.dat', 'wb') as f:
            f.write(recording.file.read())

    @classmethod
    def run(cls):
        cherrypy.quickstart(cls())

Server.run()

One critical aspect I noticed as I was distilling your question was that your HTML form isn't properly declaring the enctype. Instead, it says encrypte="multipart/form-data". I'd make sure that's enctype. After that, I expect following this same technique will work for you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top