我正在使用 NSURLConnection 使用HTTP Post向表单提交文件和一些信息。然后,此表单将文件上载到FTP服务器。 这在过去几周一直很好。但是在昨天的某个时候我必须改变一些导致它上传空文件的东西。所以发布的文件具有正确的名称和扩展名,但没有正文数据。 继承我的代码:
- (void)otherUploadingData: (NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filepath = [[NSString alloc] initWithFormat:@"%@/%@", documentsDirectory, fileName];
NSLog(@"File path for uploading method %@", filepath);
// Create data object from filepath
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
NSLog(@"The size of the file is: %lld", fileSize); //Implemented this to check that my files were not of 0 size
NSData* fileData = [[NSData alloc] initWithContentsOfFile:filepath];
//create request and set url, method
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://xx.xxx.xx"]];
[request setHTTPMethod:@"POST"];
//define charset for?
NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *endBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *tempPostData = [NSMutableData data];
[tempPostData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Sample Key Value for data
[tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"username"] dataUsingEncoding:NSUTF8StringEncoding]];
[tempPostData appendData:[@"xxx" dataUsingEncoding:NSUTF8StringEncoding]];
[tempPostData appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];
[tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"password"] dataUsingEncoding:NSUTF8StringEncoding]];
[tempPostData appendData:[@"xxx" dataUsingEncoding:NSUTF8StringEncoding]];
[tempPostData appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];
// Sample file to send as data
[tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[tempPostData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[tempPostData appendData: fileData];
[tempPostData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:tempPostData];
// now lets make the connection to the web
NSError *returnError = nil;
NSHTTPURLResponse *returnResponse = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnResponse error:&returnError];
NSString *htmlSTR = [[NSString alloc] initWithData:returnData
encoding:NSUTF8StringEncoding];
//NSLog(@"The return string: %@" , htmlSTR);
SBJsonParser *jsonParser = [SBJsonParser new];
NSMutableDictionary *jsonResp = [jsonParser objectWithString:htmlSTR error:nil];
NSInteger success = [(NSNumber *) [jsonResp objectForKey:@"success"] integerValue];
NSLog(@"Json response : %@", jsonResp);
if (success == 1) {
NSLog(@"File uploaded");
}
else {
NSLog(@"Didnt upload");
}
}
查看应用程序的日志,我知道:
继承了我的尝试:
有人对于为什么上传零文件有任何想法吗?我已经尝试了没有删除命令的代码,同样的事情发生了。服务器详细信息正确,我确实可以访问服务器。