gpt4 book ai didi

objective-c - http文件上传 objective-c

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:37 25 4
gpt4 key购买 nike

我正在尝试将图像文件上传到 http 服务器。这是代码片段

 NSData *imgData = UIImagePNGRepresentation(img);

NSString *post = [NSString stringWithFormat:@"&UserID=%@&Query=%@&fileContent=", userID, @"putImage"];

NSMutableData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

[postData appendBytes:[imgData bytes] length:imgData_len];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSURL *url = [NSURL URLWithString:@"http://ryan.verifyidonline.com/test.php"];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];

[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];

[theRequest setHTTPBody:postData];

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

此代码仅发送图像文件的前几个字符。我想我必须以某种方式对文件进行编码,但我在尝试 NSString 编码时没有任何运气。

最佳答案

http://www.cocoadev.com/index.pl?HTTPFileUpload

祝你好运,这给了我一些麻烦,但基于这里的内容(你必须通读底部,随着时间的推移提交了固定代码)创建一个干净、可重用的方法来做到这一点不应该花费太多时间。

关于objective-c - http文件上传 objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3161657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com