2013-10-24 68 views
-1

我要上傳遵循的UIImage一起登錄串,如何使用ASIFormDataRequest上傳字符串和圖像?

我怎樣才能達到這樣的目的????

我可以看到只發送圖像,但我想使用此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]; 

感謝

回答

3

有一個可用於發送參數以及

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

希望幫助方法。

+0

post value是我的字符串,而對於密鑰是服務器,如 forKey:@「post_id」 setPostValue:@「22」; – Duaan

+0

那是正確的,forKey應該是你的服務器端代碼的參數名稱,它將接收你在setPostValue中設置的值 –

+0

答案對你有幫助嗎?讓我知道你是否需要進一步的幫助。 –

相關問題