NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: @"http://test.com/upload.php"]];
[post setHTTPMethod: @"POST"];
NSString *boundary = [NSString stringWithString:@"--------------------------- 3587343183674357"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[post addValue:contentType forHTTPHeaderField: @"Content-Type"];
body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"textfile\"; filename=\"test.txt\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/multipart/form-data \r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString* str= @"teststring";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[post setHTTPBody:body];
发送文本文件后,我检查我的服务器,testtring被发送并附加为参数的一部分。不,实际上我希望它位于名为test.txt的文件中,并且位于目录中。那么如何将数据作为test.txt文件发送到目录中呢?我不希望它只是参数的一部分吗?
答案 0 :(得分:0)
您要将数据写入文档目录吗? 然后使用此代码...
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[data writeToFile:[NSString stringWithFormat:@"%@/test.txt", documentsDirectory] atomically:YES];