librelist archives

« back to archive

Binary files not getting saved correctly from request.data

Binary files not getting saved correctly from request.data

From:
Date:
2012-05-17 @ 23:21
Hi, I am trying to implement file uploads on a website in Flask and have 
the following:

@app.route('/attachments/create', methods=['POST'])
def create_attachment():
        filename = secure_filename(request.args.get('qqfile'))
        with open(os.path.join(app.config['UPLOAD_DIR'], filename), 'w') as f:
            f.write(request.data)

On the client side, I am using an AJAX upload plugin 
(http://valums.com/ajax-upload/). Now, it seems that the uploads succeed, 
however, only pure text files like source code are saved correctly. Binary
formats like .png or other file types are un-openable on the receiving 
end. I'm wondering if anyone has experienced this or perhaps I'm using 
request.data in the wrong way.

Thank you,
RH

Re: [flask] Binary files not getting saved correctly from request.data

From:
Grant Murphy
Date:
2012-05-17 @ 23:34
I'm not familiar with the ajax upload plugin. If you did it with a normal
form like this:

<form action="/attachments/create" method=post
enctype=multipart/form-data>
    <label>Attach file:</label> <input type=file name=file>
    <input type=submit value=Upload>
</form>

You would do something like this to write out the content instead of
request.data:

f.write(request.files['file'].read())

hth

Grant.


On Fri, May 18, 2012 at 9:21 AM, <Ryan.Hsu@emc.com> wrote:

>  Hi, I am trying to implement file uploads on a website in Flask and have
> the following:****
>
> ** **
>
> @app.route('/attachments/create', methods=['POST'])****
>
> def create_attachment():****
>
>         filename = secure_filename(request.args.get('qqfile'))****
>
>         with open(os.path.join(app.config['UPLOAD_DIR'], filename), 'w')
> as f:****
>
>             f.write(request.data)****
>
> ** **
>
> On the client side, I am using an AJAX upload plugin (
> http://valums.com/ajax-upload/). Now, it seems that the uploads succeed,
> however, only pure text files like source code are saved correctly. Binary
> formats like .png or other file types are un-openable on the receiving end.
> I'm wondering if anyone has experienced this or perhaps I'm using
> request.data in the wrong way.****
>
> ** **
>
> Thank you,****
>
> RH****
>

Re: [flask] Binary files not getting saved correctly from request.data

From:
Date:
2012-05-17 @ 23:35
Found the problem. The second parameter of line:

open(os.path.join(app.config['UPLOAD_DIR'], filename), 'w') as f:

should be 'wb' for writing binary files.

From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of 
Ryan.Hsu@emc.com
Sent: Thursday, May 17, 2012 4:22 PM
To: flask@librelist.com
Subject: [flask] Binary files not getting saved correctly from request.data

Hi, I am trying to implement file uploads on a website in Flask and have 
the following:

@app.route('/attachments/create', methods=['POST'])
def create_attachment():
        filename = secure_filename(request.args.get('qqfile'))
        with open(os.path.join(app.config['UPLOAD_DIR'], filename), 'w') as f:
            f.write(request.data)

On the client side, I am using an AJAX upload plugin 
(http://valums.com/ajax-upload/). Now, it seems that the uploads succeed, 
however, only pure text files like source code are saved correctly. Binary
formats like .png or other file types are un-openable on the receiving 
end. I'm wondering if anyone has experienced this or perhaps I'm using 
request.data in the wrong way.

Thank you,
RH