1

I'm trying to upload a UIImage in monotouch to my server (nodeJS).

I've tried every possible solution found on SO and elsewhere on the net to no avail.

Basically I have a UIImage which I'm converting to a byte[] using:

public byte[] GetMergedBytes(UIImage img)
    {
        byte[] filedata = null;
        using (NSData imageData = img.AsPNG()) {
            filedata = new byte[imageData.Length];
            System.Runtime.InteropServices.Marshal.Copy (imageData.Bytes, filedata, 0, Convert.ToInt32 (imageData.Length));
        }

        return filedata;
    }

Then I post this using a variety of different methods to my server including: Upload files with HTTPWebrequest (multipart/form-data)

When I try using CURL, my server responds correctly:

curl  -F "fileupload=@logo.png" -F "name=blah" http://xxx.xxx.xxx.xx/upload

For completion, I'm using nodeJS with expressJS:

app.post('/upload', function(req, res, files) {
console.log(req.files);
console.log(files);
}

With CURL I get the following from the console.log on the server:

{ fileupload: 
  { domain: null,
   _events: null,
   _maxListeners: 10,
   size: 88270,
   path: '/tmp/9ab5c9b1ea6da91e4e16ea711636b9bb',
   name: 'logo.png',
   type: 'application/octet-stream',
   hash: false,
   lastModifiedDate: Thu Jan 31 2013 07:26:43 GMT+0000 (UTC),
   _writeStream: 
   { domain: null,
    _events: null,
    _maxListeners: 10,
    path: '/tmp/9ab5c9b1ea6da91e4e16ea711636b9bb',
    fd: 9,
    writable: false,
    flags: 'w',
    encoding: 'binary',
    mode: 438,
    bytesWritten: 88270,
    busy: false,
    _queue: [],
    _open: [Function],
    drainable: true },
  length: [Getter],
  filename: [Getter],
  mime: [Getter] } }

with any other method, the same console.log returns:

{}

Any ideas? I've been going crazy over here!

UPDATE

Fixed it. I'm now using RestSharp instead and it works like a charm with very little lines of code...

byte[] filedata = GetFileBytes(file);
var client = new RestClient ("http://server");

var request = new RestRequest ("upload", Method.POST);
        request.AddParameter("name", "parameter1);
        request.AddParameter("name2", id);
        request.AddFile("file", filedata, "somename.png", "image/png");

        RestResponse response = (RestResponse)client.Execute(request);
        var content = response.Content;

        return content;
Community
  • 1
  • 1
Farid
  • 47
  • 1
  • 7
  • I've also used the popular: http://www.briangrinstead.com/blog/multipart-form-post-in-c – Farid Jan 31 '13 at 07:46
  • 1
    You can move your update into an answer and mark your question as answered (it's perfectly OK to answer your own questions on stackoverflow, see FAQ). That way other people who search for similar questions will see an answer is available (so it's more helpful that way). Welcome to SO! – poupou Jan 31 '13 at 16:09

1 Answers1

0

UPDATE

Fixed it. I'm now using RestSharp instead and it works like a charm with very little lines of code...

    byte[] filedata = GetFileBytes(file);
    var client = new RestClient ("http://server");

    var request = new RestRequest ("upload", Method.POST);
    request.AddParameter("name", "parameter1);
    request.AddParameter("name2", id);
    request.AddFile("file", filedata, "somename.png", "image/png");

    RestResponse response = (RestResponse)client.Execute(request);
    var content = response.Content;

    return content;
Farid
  • 47
  • 1
  • 7