开发者

Trying to upload image from iPhone to server using ASIFormDataRequest

开发者 https://www.devze.com 2022-12-21 20:01 出处:网络
I\'m trying to use ASIFormDataRequest to send a photo image from my iPhone app to my server. When I do so, the server sends back an error

I'm trying to use ASIFormDataRequest to send a photo image from my iPhone app to my server.

When I do so, the server sends back an error message saying, "must be an image file," implying that there is something wrong with the formatting of my image. (Of course, that might not be the problem...but it's a good place to start.) I know there's no "problem," per se, on the server, because a previous approach I used (non-ASIHTTPRequest-based) worked fine开发者_开发百科. Here's my iPhone code:

        ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL: 
[NSURL URLWithString:photoUploadURLString]]; 
        request.username = self.username; 
        request.password = self.password; 
        [request setPostValue:self.place.placeId forKey:@"place_id"]; 
        NSData* jpegImageData = UIImageJPEGRepresentation(self.photo, 
PHOTO_JPEG_QUALITY); 
        NSString* filename = [NSString stringWithFormat:@"snapshot_%@_ 
%d.jpg", self.place.placeId, rand()]; 
        [request setData:jpegImageData withFileName:filename 
andContentType:@"image/jpeg" forKey:@"snapshot[image]"]; 
        request.uploadProgressDelegate = self.progressView; 
        [request startSynchronous]; 

Anyone see anything wrong with this? (I'm fairly new to this stuff, so there could be something obvious I'm missing.)

Thanks very much.


Have you ever try addData instead of setData, It worked on my solution.

[request addData:jpegImageData withFileName: filename andContentType:@"image/jpeg" forKey:@"photos"];

Edit: By the way, which server side are you using ?

Your problem might be inside of the your key forKey:@"snapshot[image]"] try to dummy variable key like forKey:@"mysnapshot"]

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消