如何使用ASIFormDataRequest上传字符串和图像?

时间:2013-10-24 10:30:31

标签: ios objective-c asihttprequest

我想上传跟随Log字符串和UIImage,

我能实现这样的目标吗?

我可以看到只发送图像,但我想使用此ASIFormDataRequest使用POST请求类型发送图像和文本数据。

  NSLog(@"data is here %@\n%@\n%@\n%@\n%@\n",deviceID, postID, name, message, picture);


    UIImage *image= [UIImage imageNamed:@"profile_avatar.png"];
    NSString *strURL = @"https://abc.abc.com/comments/create";

    //      ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; // Upload a file on disk
   // NSString *filename1=[NSString stringWithFormat:@"friendship.jpg"];
    UIImage *image1=image;
    NSData *imageData1=UIImageJPEGRepresentation(image1, 1.0);

    [request setData:imageData1 withFileName:@"filename" andContentType:@"image/png" forKey:@"avatar"];
    [request setRequestMethod:@"POST"];

    [request addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@",[ASIHTTPRequest base64forData:[[NSString stringWithFormat:@"username123:pass123"] dataUsingEncoding:NSUTF8StringEncoding]]]];

    [request setValidatesSecureCertificate:NO];
    //[request appendPostData:body];
    [request setDelegate:self];
    [request setTimeOutSeconds:3.0];
    request.shouldAttemptPersistentConnection = NO;
    [request setDidFinishSelector:@selector(uploadRequestFinished:)];
    [request setDidFailSelector:@selector(uploadRequestFailed:)];
    [request startAsynchronous];

由于

1 个答案:

答案 0 :(得分:3)

有一种方法可用于发送参数

[request setPostValue:@"YourValue" forKey:@"YourKey"];

希望有所帮助。