1

我正在使用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");
}

}

查看应用程序的日志,我知道:

  • 它采用正确的文件
  • 该文件的大小非零
  • 触发“文件上传”日志

这是我尝试过的:

  • 手动选择文件并将其发送到方法(此时文件选择器循环运行
  • 恢复到以前的代码

有人知道为什么它可能会上传零文件吗?我试过没有删除命令的代码,同样的事情发生了。服务器详细信息是正确的,我确实可以访问服务器。

4

0 回答 0