2010-11-24 83 views
1

可以从Cocoa内提交文件吗?我的意思是我在客户端有一个文件,我想要上传到我的服务器,就像我在HTML上传字段中选择它一样。然后,我需要显示服务器在文本字段中发送的响应。Cocoa POST请求文件

这是否可能,如果可以,我可以在进度条上显示进度吗?我确实有发送普通POST请求的代码,但我不知道如何去有效地将文件发送到服务器端PHP脚本。

感谢您的帮助!

回答

3

您可以提交这样的文件的内容:通过你的PHP脚本担任

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://"]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; 

NSString *xmlString = [NSString stringWithFormat:@"%@"]; 

[request setValue:[NSString stringWithFormat:@"%d", [xmlString length]] 
forHTTPHeaderField:@"Content-length"]; 

[request setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

响应可以由代表检索(都必须执行,见的细节NSURLConnection的文档,还有一个例如):

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
+0

这似乎只适用于文本文件...那么像ZIP档案文件呢? – 2010-11-24 17:12:06