简体   繁体   中英

Uploading Image from iPhone to Server

**I went through lots of answers in stackoverflow for uploading image from iPhone to server but still couldn't come up with a solution.

It's a simple image upload with an UserID along with it, here's how my code looks**

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

request.timeoutInterval = 60;

[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];    
[request setValue:@"07ee7054-010b-44da-a14f-02e3e66800b7" forHTTPHeaderField:@"userId"];
[request imageData];

conn = [[NSURLConnection alloc]
        initWithRequest:request
        delegate:self startImmediately:NO];

[conn scheduleInRunLoop:[NSRunLoop mainRunLoop]
                forMode:NSDefaultRunLoopMode];
[conn start];

if (conn)
{
    receivedData = nil;
    receivedData = [NSMutableData data] ;
    [receivedData setLength:0];
}

This is how my server side looks,

@RequestMapping(value = "/uploadphoto", method = RequestMethod.POST, headers="Accept = */*")
public Map<String,Object> uploadPhoto(InputStream in,@RequestBody String JSONContentofPOST)
{
    return UploadPhoto(in,JSONContentofPOST);
} 

The response I'm getting is

<html>
   <head>
      <title>Apache Tomcat/7.0.53 - Error report</title>
      <style>
         <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
      </style>
   </head>
   <body>
      <h1>HTTP Status 400 - Required String parameter 'userId' is not present</h1>
      <HR size="1" noshade="noshade">
      <p><b>type</b> Status report</p>
      <p><b>message</b> <u>Required String parameter 'userId' is not present</u></p>
      <p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p>

Can someone please get me through this? Thanks in advance.

You can't send Content-Type: image/png AND string data at the same time. You need to send a multipart/form-data request instead. You'll need to set up your server to handle that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM