0
我正在做一個小型項目,我需要發送圖像文件到服務器,因爲我正在使用多部分請求。Ios多部分請求沒有獲取內容類型在服務器端
下面是我的代碼
UIImage *resizedImage1 = [img resizedImage:CGSizeMake(90.0f, 90.0f) interpolationQuality:kCGInterpolationHigh];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(resizedImage1,0.8)];
NSString *urlString = @"myUrl/accounts/add_profile_image";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
// NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; image/jpeg; boundary=%@",boundary];
NSString *accessToken=[[NSUserDefaults standardUserDefaults]objectForKey:@"userAccesstoken"];
NSLog(@"access token %@",accessToken);
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request addValue:accessToken forHTTPHeaderField:@"auth"];
NSLog(@"request has %@",request);
// NSLog(@"imagedata has %@",imageData);
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter v=1000
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"v\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"1000\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
// mz_access_token sewt token parameter
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mz_access_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%@\r\n",accessToken]dataUsingEncoding:NSUTF8StringEncoding]];
// image data parameter
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_image\"; filename=\"image.jpg\"\r\n\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
//[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
**[postbody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];**
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"body has %@",postbody);
[request setHTTPBody:postbody];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [postbody length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
NSError *errorReturned = nil;
NSError *error = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
//check netwrok
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&theResponse
error:&errorReturned];
if(data)
{
//parsing json
// again converting into ns dictionary object
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
//NSLog(@"post data %@",postbody);
// NSLog(@"response of web ser %@ :\n %@",name,jsonArray);
NSLog(@"Response data %@",jsonArray);
// return jsonArray;
}else{
NSLog(@"error %@",error);
}
// return [[NSDictionary alloc]init];
});
這是我的Web服務請求。 我設置的內容類型像
[postbody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
,但仍然在服務器端我得到@內容類型= nill * 在我們用ROR的webservice
*
服務器端請幫我解決這個問題。 在此先感謝..
試過了上面的答案,但它給了我相同的結果。它不能解決我的問題 – Raj