从iPhone应用程序上传图片 [英] upload image from iPhone application

查看:68
本文介绍了从iPhone应用程序上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从iPhone应用程序将图像上传到服务器

i am trying to upload a image to server from iPhone application

上传图片的PHP代码如下:

PHP code for upload image is following

if(isset($_POST['insertImage']))
{                   //INSERT IMAGE -------------------
     $method=safeData($_POST['insertImage']);
    if($method=='true')
    {
        if(isset($_POST['userId']))
        {

            if(isset($_FILES['imageFile']))
            {

            if($_FILES['imageFile']['type']=="image/gif" || $_FILES['imageFile']['type']=="image/bmp" || $_FILES['imageFile']['type']=='image/jpeg' || $_FILES['imageFile']['type']=='image/jpg' || $_FILES['imageFile']['type']=='image/png')
            {
                if($_FILES['imageFile']['size']<=5250000)
                {
                                                                                $userId=safeData($_POST['userId']);
                    $newImgName=rand()."a".time().".".findexts($_FILES["imageFile"]["name"]);                       imgPath="./../admin/images/";
                    move_uploaded_file($_FILES['imageFile']['tmp_name'],$imgPath.$newImgName);
                    $data.=saveImageInfo($userId,$newImgName);  
                }
                    else
                {
                  $data.="<List><ResponseCode>405</ResponseCode><Message>Maximum image size should not be more than 5mb </List>";   
                }
            }
            else
            {
                $data.="<List><ResponseCode>405</ResponseCode><Message>Invalid image format. only png,jpg,bmp formats supported</Message></List>";                                                      }
            }
            else
            {
                $data.="<List><ResponseCode>405</ResponseCode><Message>imageFile method not found</Message></List>";             
            }
                                                                    }
        else
        {
            $data.="<List><ResponseCode>405</ResponseCode><Message>userId method not found</Message></List>";   
        }
    }
    else
    {
        $data.="<List><ResponseCode>405</ResponseCode><Message>invalid insertImage argument</Message></List>";              
    }
}

我使用以下代码将图片上传到服务器

and I used following code to upload image to server

+(NSData *)setUserImage:(NSData *)userImageData UserId:(int)UserId
{
    NSString *result;
    NSData *responseData;
    @try {
        NSURL *url = [[NSURL alloc] initWithString:webAddress]; 
        NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
        [req setHTTPMethod:@"POST"];

        [req setValue:@"multipart/form-data; boundary=*****" forHTTPHeaderField:@"Content-Type"];//

        NSMutableData *postBody = [NSMutableData data];
        NSString *stringBoundary = [NSString stringWithString:@"*****"];

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"insertImage\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"true"] dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];


        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userId\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"%d",UserId] dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];

        [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imageFile\"; filename=\"myimagefile.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];

        //[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imageFile\"; filename=\"myimagefile.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[NSData dataWithData:userImageData]];// dataUsingEncoding:NSASCIIStringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];

        [req setHTTPBody: postBody];//putParams];   

        NSHTTPURLResponse* response = nil;  
        NSError* error = [[[NSError alloc] init] autorelease];  

        responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];  
        result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        if(isInDebugMode)
            NSLog(@"Result: %@", result);

        [url release];
        [req release];

        IceCreamManFinderAppDelegate *delegate1=(IceCreamManFinderAppDelegate *)[UIApplication sharedApplication].delegate;
        if(error.domain!=nil)
        {
            NSString *errorDesc=[[error userInfo] objectForKey:@"NSLocalizedDescription"];
            delegate1.globalErrorMessage=errorDesc;
            return nil;
        }
        else
        {
            delegate1.globalErrorMessage=nil;
        }
    }
    @catch (NSException* ex) {
        NSLog(@"Error: %@",ex);
    }
    return responseData;
}

从上面的代码我得到以下来自服务器的回复

from above code i get following response from server

图片格式无效。只有png,jpg,bmp格式

我尝试了很多但没有成功。

i tried lot but not success.

请建议哪里我错了?

推荐答案

您编写的服务器代码只接受具有特定指定内容类型的数据,但之后您从未添加过数据的内容类型。只需为您的图像数据发出正确的内容类型:

You wrote the server code to only accept data with certain specified content types, but then you never added the content type to your data. Simply emit the correct content type for your image data:

// Emit the content type here as well
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imageFile\"; filename=\"myimagefile.png\"\r\nContent-Type: image/png\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];

[postBody appendData:[NSData dataWithData:userImageData]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];

错误实际上来自您自己的服务器响应,所以只需按照您的控制流程服务器代码为您提供原因。

The error is actually coming back from your own server response, so simply following the flow of control on your server code gives you the cause.

这篇关于从iPhone应用程序上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆