1
我正在使用NSURLConnection
發送相同的數據(NSString
)到服務器,我想添加一個圖像或文件,所以什麼是內容類型的價值?我如何上傳文件和其他參數使用NSURLConnection
編碼
- (NSData *)encodingData:(NSMutableDictionary *)dictionary
{
NSMutableArray *arrayPosts = [[NSMutableArray alloc] init];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *encodedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedValue = [obj stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[arrayPosts addObject:[NSString stringWithFormat:@"%@=%@",encodedKey,encodedValue]];
}];
NSString *encodedArrayPosts = [arrayPosts componentsJoinedByString:@"&"];
return [encodedArrayPosts dataUsingEncoding:NSUTF8StringEncoding];
}
發送數據
- (void)startAsyncRequest
{
// Enable the network activity indicator in the status bar
[self enableActivityIndicatorInStatusBar];
// Setting of the request
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:self.url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[urlRequest setHTTPMethod:self.method];
[urlRequest setHTTPBody:[self encodingData:self.dictionaryPosts]];
// Send the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (connection) {
// Connection succeded
self.receiveData = [NSMutableData data];
} else {
// Connection Failed
self.error = @"Connection Failed";
// Inform the user that the connection failed
[self.delegate requestFailed:self];
}
}
但我怎麼能混合他們兩個我的意思是文件和簡單的數據? –
@上面的Tamim.Z代碼**數據**的示例鍵值爲簡單數據,而另一個爲文件 – dianz