redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

Error when uploading text file through multipart request #77

Closed cgarciae closed 9 years ago

cgarciae commented 9 years ago

I've setup my route to accept file uploads

@app.Route("/private/file", methods: const [app.POST], allowMultipartRequest: true)
@Encode()
Future<IdResp> newFile(@app.Attr() MongoDb dbConn, @app.Body(app.FORM) Map form) async

and it works fine for most file types except "text" files like .txt or .xml. I specifically need to upload an xml as a normal file to store it in GridFS so my clients can download the file later. I am getting this error

type 'String' is not a subtype of type 'List' of 'content'.

package:redstone/src/http_body_parser.dart 12:60     HttpBodyFileUpload.HttpBodyFileUpload
package:redstone/src/http_body_parser.dart 71:26     _parseRequestBody.asFormData..
dart:async                                           _StreamController.close
package:mime/src/bound_multipart_stream.dart 308:42  BoundMultipartStream._parse
package:mime/src/bound_multipart_stream.dart 109:24  BoundMultipartStream.BoundMultipartStream..
dart:isolate                                         _RawReceivePortImpl._handleMessage

It seems that its interpreting the file as a String.

luizmineo commented 9 years ago

If the form element was read as a String, then the request is not multipart, or the corresponding form element is a text input, not a file input. Check if the form used to upload the file has the enctype='multipart/form-data' attribute, and the corresponding element is a file input.

cgarciae commented 9 years ago

@luizmineo Sadly enctype is multipart/form-data and the input is of type file. Somewhere along the way its converting text files to string.

<form class="panel" enctype="multipart/form-data">
    <input name="file" type="file"><br>
    <button class="button tiny radius" ng-click="uploadModel (info, 'xml', $event)">
        Upload
    </button>
</form>

I am doing something like this to send the form

HttpRequest.request
(            
    path,
    method: method,
    sendData: new dom.FormData (form)
)

where form is the FormElement.

luizmineo commented 9 years ago

You are right. text files are being converted to strings. To keep compatibility with the http_server package, I've changed HttpBodyFileUpload.content to dynamic. This fix is available in Redstone v0.5.21

cgarciae commented 9 years ago

Hadn't seen this. Thanks a lot!!! I'd been creating extensionless files as a fix. Will test right now :)

cgarciae commented 9 years ago

@luizmineo The .content problem was solved, but now I am getting a similar problem when trying to upload an xml

type 'String' is not a subtype of type 'List' of 'data'.

Now .data is the problem.

luizmineo commented 9 years ago

Can you try this in v0.6.0-beta.1?

cgarciae commented 9 years ago

@luizmineo Ok, I'll get that version. Any braking changes?

luizmineo commented 9 years ago

Yes. Check the changelog for details