Iphone 目标-c-不';t上传文件';内容
我想将本地word(或rtf)文件上载到我的服务器。上载没有内容的文件。没有错误代码或任何东西Iphone 目标-c-不';t上传文件';内容,iphone,objective-c,upload,Iphone,Objective C,Upload,我想将本地word(或rtf)文件上载到我的服务器。上载没有内容的文件。没有错误代码或任何东西 NSString *urlString = @"http://myserver.com/upload-c.php"; NSString *filename=@"xx.docx"; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"xx.docx"]]; NSMutableURLRequest *request =
NSString *urlString = @"http://myserver.com/upload-c.php";
NSString *filename=@"xx.docx";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"xx.docx"]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@",returnString);
一个明显的问题是如何初始化
数据
变量
假设“xx.docx”是您希望上载的文件,则有两个问题
NSURL
,因此需要使用fileURLWithPath:
,而不是URLWithString:
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"xx" ofType:@"docx"];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
此外,这一行:
[body appendData:[NSData dataWithData:data]];
应该是:
[body appendData:data];
并摆脱对stringWithString:
的调用。例如:
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
应该是:
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *boundary = @"---------------------------14737809831466499882746641449";
以及:
应该是:
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *boundary = @"---------------------------14737809831466499882746641449";
“您需要指定文件的完整路径,而不仅仅是文件名。”它与.h和.m文件是同一目录吗?它不在子目录中?您需要在此处处理应用程序的运行时环境,而不是项目。在运行时,文件位于用户设备上的某个特定路径。您需要确定文件的完整路径。文件在哪里?它是在应用程序的资源包中还是保存在“文档”文件夹(或应用程序沙箱中的其他文件夹)中?如何在运行时找到文件的完整路径?有办法找到它吗?它在我的项目文件夹中。我应该在哪里找到它?