0
我正在使用下面的代碼上傳圖片到php服務器,但IOException消息顯示任何人都可以幫助我嗎?圖片上傳iphone到php服務器
- (void)uploadFileToURL:(NSURL *)url FileData:(NSData *)fileData FileParameterName:(NSString *)fileParameterName FileName:(NSString *)fileName withParameters:(NSDictionary *)parameters
{
NSMutableString *params = nil;
NSURL *finalURL = url;
if (parameters != nil)
{
params = [[NSMutableString alloc] init];
for (id key in parameters)
{
NSString *encodedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
CFStringRef value = (CFStringRef)[[parameters objectForKey:key] copy];
// Escape even the "reserved" characters for URLs
// as defined in http://www.ietf.org/rfc/rfc2396.txt
CFStringRef encodedValue = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
value,
NULL,
(CFStringRef)@";/?:@&=+$,",
kCFStringEncodingUTF8);
[params appendFormat:@"%@=%@&", encodedKey, encodedValue];
CFRelease(value);
CFRelease(encodedValue);
}
[params deleteCharactersInRange:NSMakeRange([params length] - 1, 1)];
NSString *urlWithParams = [[url absoluteString] stringByAppendingFormat:@"?%@", params];
finalURL = [NSURL URLWithString:urlWithParams];
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:fileData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
[request setHTTPMethod:@"POST"];
[self startConnection:request];
}