简体   繁体   中英

POST multipart form using HttpWebRequest in C# to mimic curl

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM