文件上传到iphone编程中的HTTP服务器

BP.*_*BP. 58 iphone file-upload http

任何人都可以提供一些链接或示例,使用iphone API将文件上传到HTTP服务器.

Bra*_*ker 74

下面的代码使用HTTP POST将NSData发布到Web服务器.您还需要PHP的一些知识.

NSString *urlString = @"http://yourserver.com/upload.php";
NSString *filename = @"filename";
request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:YOUR_NSDATA_HERE]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
Run Code Online (Sandbox Code Playgroud)

  • @Marc`14737809831466499882746641449`是随机边界.你可以使用任何东西,但每个人总是使用`14737809831466499882746641449`.我完全不知道为什么,也许它用在一些苹果的示例代码中.有人有什么想法吗? (4认同)
  • 这很好看.我没有看到任何PHP.我认为你的意思是HTTP. (3认同)

Jan*_*les 32

ASIHTTPRequest是网络API的绝佳包装器,可以非常轻松地上传文件.这是他们的例子(但你也可以在iPhone上这样做 - 我们将图像保存到"磁盘",然后上传它们.

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
Run Code Online (Sandbox Code Playgroud)

  • 可悲的是,似乎ASI在IOS5中不起作用,不再受支持. (2认同)

Tie*_*eme 7

我使用ASIHTTPRequest很像Jane Sales的答案,但它不再在开发中,作者建议使用AFNetworking等其他库.

老实说,我认为现在是时候开始寻找其他地方了.

AFNetworking效果很好,让你可以使用很多块(这是一个很大的缓解).

这是github上的文档页面上的图片上传示例:

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];
Run Code Online (Sandbox Code Playgroud)