1
我想知道如何使用目標C將多個圖像上傳到服務器。 下面是我嘗試使用的代碼片段。將多個圖像上傳到服務器IOS
-(void)uploadImage:(NSMutableArray *)image andMessageBO:(MessageBO *)message
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@PostImageServlet?",kPostImageUploadWebServiceURL]];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:url];
[postRequest setHTTPMethod:@"POST"];
[postRequest setTimeoutInterval:60.0];
NSString *stringBoundary = @"0xKhTmLbOuNdArY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSData *imageData =UIImagePNGRepresentation(image);
NSMutableData *postBody = [[NSMutableData alloc] init];
[postBody appendData:[[NSString stringWithFormat:@"isFormField=true"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%d",messageBO.messageId] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profilepic\"; filename=\"%d_postImage.png\"\r\n",messageBO.messageId] dataUsingEncoding:NSUTF8StringEncoding]];
for (int i = 0; i < [image count]; i++)
{
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profilepic\"; filename=\"%d_postImage%d.png\"\r\n",messageBO.messageId,i] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[image objectAtIndex:i]];
}
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:imageData]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
DebugLog(@"IMge:%@",returnString);
// [self removeOfflineImage:expenseBO];
}
感謝
令人驚歎的代碼。我只是試了一下,它對我很有用 –
@丹,很高興它爲你工作..乾杯! :) – 0yeoj