0

如何使用 ASIFormDataRequest 将图像上传到服务器?我试图上传一张图片,它在插入时没有显示任何错误。并在我的请求中完成了方法,我得到了成功的响应。所以问题是,当我得到我插入的数据时,我无法获得我插入的图像,但我也得到了图像路径。

这是我的代码

NSString *globalurl =[[NSUserDefaults standardUserDefaults]valueForKey:@"Global_Url"];

NSString *urlStr=[NSString stringWithFormat:@"%@/InsertUserInfo?User_Name=%@&User_Pwd=%@&User_FName=%@&User_LName=%@&User_Mail=%@&User_Phno=%@&User_Address=%@&User_Dob=%@&Ic_No=%@&User_Active=%@&User_Questions=%@&User_Ans=%@",globalurl,usernameTxtFld.text,pwdTxtFld.text,firstName.text,lastName.text,email.text,phonNO.text,address.text,dateOfbirth.text,nricNo.text,@"10001",@"",@""];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request addData:dataImage withFileName:@"Image.png" andContentType:@"image/png" forKey:@"photo"];
//check if passedData is nil
if (dataImage == nil)
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title:iDnil" message:@"nil" delegate:nil cancelButtonTitle:@"fgff" otherButtonTitles:@"erer", nil];
    [alert show];
}
[request startAsynchronous];

 -(void)requestFinished:(ASIHTTPRequest *)request {

NSString *receivedString = [request responseString];
NSLog(@"received messge %@",receivedString);
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Message" message:receivedString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
 }


      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
     {
dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
NSLog(@"dict is %@",info);
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];

   [self dismissModalViewControllerAnimated:YES];
}
4

2 回答 2

0

我已经上传了一个示例应用程序执行文本文件、图像、视频、队列中的图像上传、视频到 GitHub 在此链接上。你可以考虑看看它。

ASI-http-request 有一个优化选项,可以立即从您的磁盘流式传输您的图像文件,这是我在这个库中看到的最好的。请考虑使用以下代码片段来创建 ASIFormDataRequest。

    // Initilize Variables
    NSURL * url = [NSURL URLWithString:IMAGE_POST_URL];

    // Add Image
    NSString *path = [[NSBundle mainBundle]pathForResource:@"image1" ofType:@"jpg"];

    // Get Image
    NSData *imageData = [[NSData alloc]
                         initWithContentsOfFile:path];

    // Return if there is no image
    if(imageData != nil){

        //Request-1
        ASIFormDataRequest * request1;
        [request1 setTag:201];

        request1 = [[ASIFormDataRequest alloc] initWithURL:url];
        [request1 setPostValue:@"ID = 201" forKey:@"image_upload"];
        [request1 setFile:path forKey:@"image1_file"];
        [request1 setDelegate:self];
        [request1 setDidFinishSelector:@selector(imageUploadDidFinish:)];
        [request1 setDidFailSelector:@selector(imageUploadDidFail:)];
        [request1 setTimeOutSeconds:500];
        [request1 setShouldStreamPostDataFromDisk:YES];
}
于 2013-09-27T12:33:44.670 回答
0

您可以使用 [request setFile:@"YOUR IMAGE FILE PATH" forKey:@"photo"]; 而不是帖子[request addData:dataImage withFileName:@"Image.png" andContentType:@"image/png" forKey:@"photo"]; 中的说明。它对我来说就像一个魅力:-)

于 2013-09-27T12:17:42.377 回答