1

I need to POST a request. The request has 3 parameters, 'email_id' , 'location' and 'image_data'. The value for 'image_data' contains NSData jpeg representation of a UIImage. The request must be submitted using a content type of multipart/form-data. How can I create the NSMutableRequest for posting this request? How should I set the boundary? Is the boundary required for the entire packet or is it enough only for the image part?

4

3 回答 3

2

这个链接:

http://iphone.zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

应该包含你需要的一切。你需要扩展 PHP 脚本来处理你的非图像参数,但这是我使用的。:)

编辑:注意到上一个链接已损坏。新的工作

于 2010-09-01T07:39:28.600 回答
2

如果我是你,我会查看 ASIHTTPRequest 库。它是 Cocoa 的 HTTP 客户端库,它让那些通过 iPhone 应用程序进行大量 Web 交互的人的生活变得更加轻松。

这是我,使用 ASIFormDataRequest(ASIHTTPRequest 的一个组件)从我的 iPhone 应用程序上传图像。客户对这些图像的称呼是“标记”——它们将出现在当地用户拍摄的本地图片地图上。邀请用户“做你的标记”。你可以想象随之而来的欢闹。

Anyhoo,self.mark是我的 Mark 类的一个实例,它封装了关于我正在上传的图像和详细信息包的数据。我在此方法的第一行中使用了一个数据管理器单例,其中包含当前的 CLLocation,因此我可以获得这张图片的地理编码信息。

请注意,我不关心编码类型或多部分边界。图书馆处理所有这些。

-(void)completeUpload
{
    CLLocation *currentLoc = [DataManager sharedDataManager].currentLocation;
    self.mark.latitude = [NSNumber numberWithDouble:currentLoc.coordinate.latitude];
    self.mark.longitude = [NSNumber numberWithDouble:currentLoc.coordinate.longitude];

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:
                                   [NSURL URLWithString:[NSString stringWithFormat:@"%@image_upload.php", WEBAPPURL]]];


    [request setPostValue:self.mark.titleText forKey:@"title"];
    [request setPostValue:self.mark.descriptionText forKey:@"description"];
    [request setPostValue:self.mark.event.guid forKey:@"event"];
    [request setPostValue:self.mark.latitude forKey:@"latitude"];
    [request setPostValue:self.mark.longitude forKey:@"longitude"];
    [request setPostValue:self.mark.project forKey:@"project"]; 
 [request setPostValue:[[NSUserDefaults standardUserDefaults] valueForKey:@"userID"] forKey:@"user_id"];
    request.timeOutSeconds = 120;

    int i = 1;
    for (NSString *tag in self.tags) {
        [request setPostValue:tag forKey:[NSString stringWithFormat:@"tag-%d", i]];
         i++;
    }

    NSData *imageData = UIImagePNGRepresentation(self.mark.image);
    NSData *thumbData = UIImagePNGRepresentation(self.mark.thumbnail);
    [request setData:imageData forKey:@"file"];
    [request setData:thumbData forKey:@"thumb"];

    self.progress.progress = 20.0;
    [request setUploadProgressDelegate:self.progress];
    request.showAccurateProgress = YES;

    request.delegate = self;
    [request startAsynchronous];
}

编辑:顺便说一句,我发布到的 PHP 脚本在 HTTP 身份验证之后。ASI 缓存了这些凭据,并且我之前提供了它们,因此我不必在这里再次提供它们。我注意到,否则,按照这篇文章和相应 PHP 脚本的编写方式,任何人都可以伪造其他任何人的用户 ID 值并在他们的用户名下发布任何内容。当您构建这样的应用程序时,您必须考虑 Web 应用程序的安全性,这与它是一个网站没有什么不同。它是一个网站,实际上,只是通过非传统客户端浏览。

于 2010-09-01T12:34:39.880 回答
2
NSData *imageData=UIImageJPEGRepresentation(imageview.image, 1.0);
NSString *filename=@"nike.jpg"

NSString *urlString = @"http://xyz.com/file_upload/file1.php";
NSMutableURLRequest *request =[[[NSMutableURLRequest alloc] init] autorelease];
[请求 setURL:[NSURL URLWithString:urlString]];
[请求 setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"-----------------99882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];
    [请求 addValue:contentType forHTTPHeaderField:@"Content-Type"];

     NSMutableData *body = [NSMutableData 数据];
     NSMutableString * string = [[NSMutableString alloc] init];
    [字符串 appendFormat:@"\r\n\r\n--%@\r\n", 边界];
    [string appendFormat:@"Content-Disposition: form-data; name=\"emailid\"\r\n\r\n"];
    [字符串 appendFormat:@"pradz39@gmail.com"]; //价值
    [正文 appendData:[字符串 dataUsingEncoding:NSUTF8StringEncoding]]; // 加密整个正文   

    [正文 appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding: NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [正文 appendData:[NSData dataWithData:imageData]];

    [正文 appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [字符串释放];

    [请求 setHTTPBody:body];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returnedResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
于 2010-09-21T04:16:29.503 回答