gpt4 book ai didi

objective-c - AFNetworking 从 iOS 上传图片到 API

转载 作者:可可西里 更新时间:2023-11-01 03:32:31 26 4
gpt4 key购买 nike

我在上传带有数据的图像时遇到问题。此代码允许我上传数据就好了:

    NSURL *url = [NSURL URLWithString:@"my_base_url"];
AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];

//depending on what kind of response you expect.. change it if you expect XML
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:
_topicText.text,@"Topic",
@"1",@"Category",
@"",@"MainMedia",
@"1",@"Creator",
nil];
[client postPath:@"apidebate/debates" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure");
}];

但是,我未能成功上传包含该数据的图像:

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

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];

我在工作中可能做错了什么??

编辑(添加服务器代码)

后端是 Codeigniter,内置 REST 客户端 Phil Sturgeon(很棒的库,顺便说一句)。我几乎可以肯定这不是服务器代码,因为此方法的第一行向我发送了一封电子邮件。当我尝试使用上面的代码发布图片时,电子邮件永远不会出现。所以,它似乎还没有到达终点。

//CONTROLLER FROM API
function debates_post()
{
mail('myemailaddress@gmail.com', 'Test', 'Posted');
$tmp_dir = "images/posted/";

if(isset($_FILES['MainMedia'])){
$SaniFileName = preg_replace('/[^a-zA-Z0-9-_\.]/','', basename($_FILES['MainMedia']['name']));

$file = $tmp_dir . $SaniFileName;
move_uploaded_file($_FILES['MainMedia']['tmp_name'], $file);
}
else
$SaniFileName = NULL;

$data = array('Topic'=>$this->post('Topic'), 'MainMedia'=>$this->post('MainMedia'), 'Category'=>$this->post('Category'), 'Creator'=>$this->post('Creator'));
$insert = $this->debate->post_debate($this->post('Topic'), $SaniFileName, $this->post('Category'), $this->post('Creator'));
if($insert){
$message = $this->db->insert_id();
}
else{
$message = 'Insert failed';
}

$this->response($message, 200);
}

//MODEL
function post_debate($Topic=NULL, $MainMedia='', $Category=NULL, $Creator=NULL){
$MainMedia = ($MainMedia)?$MainMedia:'';
$data = array(
'Topic' => $Topic,
'MainMedia' => $MainMedia,
'Category' => $Category,
'Creator' => $Creator,
'Created' => date('Y-m-d h:i:s')
);
return $this->db->insert('debate_table', $data);
}

最佳答案

好的,显然使用 fileName:@"MainMedia" 作为文件名给我留下了一个无扩展名的上传文件,这导致了服务器端错误。添加 .jpg""MainMedia.jpg" 修复了这个问题。

关于objective-c - AFNetworking 从 iOS 上传图片到 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12035822/

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