dongyong8071 2014-10-08 07:28
浏览 126

将图像从ios上传到PHP到[关闭]

I am having an app in which I am uploading some data to web server.

Here is the code that I got from the links from google.

UIImage * img = [UIImage imageNamed:@"register_btn.png"];

    NSData *imageData = UIImageJPEGRepresentation(img, 90);
    NSString *urlString = @"http://tes.in/testservices/User.php";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"------------------------1473780983146499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"
--%@r
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data;filename=\a.jpg\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/json

"]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString =[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        //profile_photo_link
    NSLog(@"%@",returnString);

But I am not getting what exactly I need to pass here in the code.

My parameter for photo upload is "photo_link". I have given the link for php webservice.

I know there are lots of questions on this but I don't know where to pass my parameters and how to check the request string.

If possible please anyone can give me the code for PHP to decode image.

Please help me.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doukuipei9938 2014-10-08 07:41
    关注

    Try this code :

    -(void)uploadImageOnServer:(NSData *)imageData forId:(NSString *)imageId
    {            
        NSURL *strURL = @"http://webserver.com/....";//give your URL here
    
        // setting up the request object now
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:strURL];
        [request setHTTPMethod:@"POST"];
    
        NSString *boundary = @"---------------------------147378098314876653456641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
        /*
         now lets create the body of the post
         */
        NSMutableData *body = [NSMutableData data];
    
        [body appendData:[[NSString stringWithFormat:@"
    --%@
    ",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *stringData = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Documents\"; filename=\%@.jpg
    ",imageId];
        [body appendData: [stringData dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream
    
    " dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"
    --%@--
    ",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // setting the body of the post to the request
        [request setHTTPBody:body];
    
        // now lets make the connection to the web
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    
        NSLog(@"%@",returnString);
    }
    

    "Documents" is the folder name where you want to save an image to the backend server.

    Good Luck!

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画