如何在NSString中提供fileName

时间:2012-03-30 05:17:02

标签: iphone objective-c nsstring filenames

我正在从服务器上传音频文件,我希望文件的名称应该像我一样

NSString *fileName=[NSString stringWithFormat:firstName,lastName,pateintID]

我希望这个文件名像这样传递给服务器

  [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".wav\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];

2 个答案:

答案 0 :(得分:2)

NSString *fileName=[NSString stringWithFormat:@"%@%,@%,%@",firstName,lastName,pateintID];

或者这将输出firstName_lastName_pateintID.mp3

NSString *fileName=[NSString stringWithFormat:@"%@%_@%_%@.mp3",firstName,lastName,pateintID];

如果要上传音频文件,请使用AFNetworking框架。使用Progress Callback示例检查文件上载。

答案 1 :(得分:0)

NSString *fileName=[NSString stringWithFormat:@"%@%,@%,%@",firstName,lastName,pateintID];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"audio\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[fileName dataUsingEncoding:NSUTF8StringEncoding]];//file name is ur typed data

音频文件发送到服务器

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".wav\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:AudioNSDataName]];  //AudioNSDataName is ur Audio NSData variable.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];