Question

My app has a python (bottle.py) backend and an html/jquery frontend. I am getting a text file using 'input' in a form and uploading it in background, using event handler on a python side. The event handler then parses the file (calling file.readline() repetitively) and returns some result in json form back.

Everything goes well until I need to parse the file in python.

Here is my javascript:

function UploadFile() {
    var file = $('#data')[0].files[0];
    var formData = new FormData();
    formData.append("file", file);
    $.ajax({
        url: 'plates',
        type: 'POST',
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data){
            ...
        }
    });
}

When I recieve the POST request on the python side, I can read it only with request.body.read().decode(). If I am trying request.files.data.file.read(), I am getting the 'broken pipe' error:

socket.error: [Errno 32] Broken pipe

I can just get the text with request.body.read().decode(), save them to file and then load it and parse. But I would like to avoid creating files that are not necessary to create, and want to do everything in memory.

Why does request.body.read().decode() work and request.files.data.file.read() doesn't? Is there any way to parse the file with my existing function while not writing it to a disk?

Was it helpful?

Solution

I suggest making sure that your request is submitted with the content-type "multipart/form-data". The default is "application/x-www-form-urlencoded", which might not be what bottle expects when trying to parse a file.

RFC 2388 is a good (albeit thick) source of information if you want to learn more about handling multipart forms in HTTP (including files)

Also, here's a good discussion about file upload with jQuery.

OTHER TIPS

I have absolutely no idea why the two calls are different (I use django), but the way you parse it without writing to disk is by getting a new BytesIO instance, writing to that, and using that in place of your file handle. Just a partial solution.

If it is just text, consider using StringIO instead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top