无法上传图片

I'm delevoping an app on iPhone. My partner sends me a curl command like this

curl -F "clothing_item[photo]=@dress1.jpg" -F "clothing_item[name]= pink_dress" http://www......com/shop_items.json?auth_token=abc_xyz_123

and asks me to implement a function which responses to upload an image to server. I try

forge.ajax({
    url: "http://www........com/shop_items.json",
    type: "POST",
    dataType: 'json',
    data: {
        "auth_token": token,
        "clothing_item[photo]": imageFile,
        "clothing_item[name]": id + "-" + d.getTime().toString()            
    },

    success: function(data) {

    },
    error: function(error) {

    }
});

With imageFile is a file from camera. This post is successful, but there is no image uploaded to server.

Also try:

forge.ajax({
    url: "http://www........com/shop_items.json",
    type: "POST",
    dataType: 'json',
    file:[imageFile];
    data: {
        "auth_token": token,
        "clothing_item[photo]": imageFile,
        "clothing_item[name]": id + "-" + d.getTime().toString()            
    },

    success: function(data) {

    },
    error: function(error) {

    }
});

but it doesn't work to.

Any advice was appreciated

Here are a few guides to help you debug though it's really hard to know what exactly is the issue here.

  • Open Charles Proxy and run the iOS emulator. Check what's getting posted on the clothin_item[photo] field
  • Use the Trigger.IO Catalyst remote debugger to look at the Network tab, and maybe invoke similar requests manually
  • From looking at ajax request API it seems like the key is actually 'files' and not 'file'. If I understand correctly, you shouldn't post the image on the other field 'clothing_item[photo]'. Also (again, if I understand correctly), the binary file field name is 'files' and cannot be customized.