Ios NSURLConnection上载空文件
我正在使用NSURLConnection使用HTTP Post向表单提交文件和一些信息。然后,此表单将文件上载到FTP服务器。 这在过去的几周里效果不错。然而,在昨天的某个时候,我一定改变了一些东西,导致它上传空文件。因此,发布的文件具有正确的名称和扩展名,但没有正文数据。 这是我的密码:Ios NSURLConnection上载空文件,ios,objective-c,nsurlconnection,Ios,Objective C,Nsurlconnection,我正在使用NSURLConnection使用HTTP Post向表单提交文件和一些信息。然后,此表单将文件上载到FTP服务器。 这在过去的几周里效果不错。然而,在昨天的某个时候,我一定改变了一些东西,导致它上传空文件。因此,发布的文件具有正确的名称和扩展名,但没有正文数据。 这是我的密码: - (void)otherUploadingData: (NSString *)fileName { NSArray *paths = NSSearchPathForDirectoriesInDomains
- (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");
}
}
查看应用程序的日志,我知道:
- 它取的是正确的文件
- 该文件的大小不为零
- 已触发“文件上载”日志
- 手动选择文件并将其发送到方法(在文件选择器在循环上运行时)
- 恢复到以前的代码
有人知道为什么会上传零文件吗?我在没有delete命令的情况下尝试了代码,同样的情况也发生了。服务器详细信息是正确的,我确实可以访问服务器。为什么不使用Wireshark检查传输到服务器的内容?使用GIT找出您更改了什么?如果没有服务器正确地接收到文件名,那么文件内容格式可能有问题。您是否可以尝试添加内容长度?另一种尝试方法:添加内容传输编码(并使用二进制或base64编码)@SergiySalyuk我已经实现了这一点,我的57字节文件的帖子长度为442。我不认为文件内容有错,因为我没有触及它写入/压缩文件的方式。另外,当我从organizer下载文件时,它们应该是这样的。@MartinR将在一小时内获得更多服务器信息。Wain,我也会看看。谢谢