0

I'm trying to write a little flex app that has a paint/canvas type feature to draw an image, which I then want to post to a rails server side. I'm following the post here, but can't get as far as he did due to the following error:

NoMethodError (undefined method `rewind' for #):

I googled that and found this which says the problem is due to an empty filename, but I thought I had that from the example. However, I altered the example to simplify the post by reducing the form parameters, but I could have easily messed something up since I really don't know what I'm doing with this multipart form content.

I was hoping to at least log the request params, but unfortunately I can't, since it's failing before being routed and due to my inexperience with rails. I'll ask that in a separate question and hopefully be able to edit this question with the request params.

Here's my code:

public static function sendPic(simplePaint : SimplePaint) : void {
        var jpgSource:BitmapData = new BitmapData (simplePaint.width, simplePaint.height);
        jpgSource.draw(simplePaint);

        var jpgEncoder:JPEGEncoder = new JPEGEncoder(85);
        var ba:ByteArray = jpgEncoder.encode(jpgSource);

        var request:URLRequest = new URLRequest("../drawings");
        request.method = URLRequestMethod.POST;
        var boundary : String = "----------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7";
        request.contentType = "multipart/form-data; boundary=" + boundary;
        request.data = getMultiPartRequestData(boundary, 'drawing', 'mypic.jpg', ba);


        var urlLoader:URLLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
        urlLoader.load(request);
    }

    private static function getMultiPartRequestData(boundary:String,
                                             resourceName:String,
                                             filename:String,
                                             bytes:ByteArray):ByteArray {
        //Alert.show("haha");
        var lf:String = "\r\n";

        var part1:String = '--' + boundary + lf + 
            'Content-Disposition: form-data; name="Filename"' + lf + lf +
            '{0}' + lf + 
            '--' + boundary + lf ;
        part1 += 'Content-Disposition: form-data; name="commit"' + lf + lf +  
                 'Create' + lf + 
                 '--' + boundary + lf +                      
                 'Content-Disposition: form-data; name="{1}[pic] ";' +  
                 'filename="{0}"' + lf +                         
                 'Content-Type: application/octet-stream' + lf + lf     

        var part2:String = '--' + boundary + lf + 
                           'Content-Disposition: form-data; name="Upload"' + lf + lf +
                           'Submit Query' + lf +
                           '--' + boundary + '--'

        var result:ByteArray = new ByteArray();
        // Filling in the parameters as per comment above
        result.writeMultiByte(StringUtil.substitute(part1, 
                                                    filename,
                                                    resourceName), "ascii");
        result.writeBytes(bytes,0,bytes.length);
        result.writeMultiByte(part2, "ascii");
        return result;
    }
Community
  • 1
  • 1
user26270
  • 6,904
  • 13
  • 62
  • 94

1 Answers1

0

The answer was in the answer of the earlier linked question. I had just missed it, or undid it, in my copy-paste-edit of that answer.

The code that needs to change is this part :

'Content-Disposition: form-data; name="{1}[pic] ";' +  
                 'filename="{0}"' + lf +                         
                 'Content-Type: application/octet-stream' + lf + lf 

it needs to be changed to:

'Content-Disposition: form-data; name="{1}[pic]"; ' +  // space moved here
                 'filename="{0}"' + lf +                         
                 'Content-Type: application/octet-stream' + lf + lf   

When you move the space to after the semicolon, it works.

user26270
  • 6,904
  • 13
  • 62
  • 94