Error: Network Connection Lost - saving form data (file) to R2 bucket

I have this handler in my worker:

    const data = await event.request.formData();
    const key = data.get('filename');
    const file = data.get('file');
    if (typeof key !== 'string' || !file) {
      return res.send(
        { message: 'Post body is not valid.' },
    await BUCKET.put(key, file);

    return new Response(file);

If I comment out the await BUCKET.put(key, file); line, then I get the response of the file as expected. But with that line in the function, I get the error:

`Uncaught (in promise) Error: Network connection lost.`

I have confirmed that by changing the put to a get, I can retrieve files from that bucket, so there doesn’t seem to be a problem with the connection itself.

Right now I’m having to do a workaround in order to get both the filename and the file. I’m adding the filename as a query parameter in the URL, so that I can get that data separately, and just add the file as a binary in the POST data. And that’s working.

I would much prefer to be able to add them both as form data, if that’s possible.

Make sure that your compatibility_date isn’t before the default date for when FormData() supported file -

I’m not entirely sure what you’re returning - maybe the file object is consumed by .put.

Try something like this.

const object = await BUCKET.put(key, file, {
    httpMetadata: event.request.headers,

return new Response(null, {
    headers: {
        'etag': object.httpEtag,

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.