开发者社区> 问答> 正文

iOS 上传图片报错

你好,我根据后台给的 policy, s ignature, oss_access_key_id, url 我发起http请求,post图片上传到阿里云得到的错误码始终是400或者403;如果您有可行的解决方案,非常感谢!
代码片段如下:
- (void)postOSSImageKeybucketName:(NSString *)oss_access_key_id url:(NSString *)url policy:(NSString *)policy Signature:(NSString *)signature{

    if (_currentImg == nil || !_currentImg) {
        return;
    }
    //2. 图片名字
    NSString *fileName = [ShareMethod ret20bitString];
    fileName = [fileName stringByAppendingString:@".jpg"];
    //3. 图片二进制文件
    NSData *imagedata = UIImageJPEGRepresentation(_currentImg, 0.00001);
    //4. 发起网络请求
    MySessionManager *manager = [MySessionManager manager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:fileName          forKey:@"key"];
    [dic setValue:oss_access_key_id forKey:@"OSSAccessKeyId"];
    [dic setValue:policy            forKey:@"policy"];
    [dic setValue:signature         forKey:@"Signature"];
    [dic setValue:imagedata         forKey:@"file"];
//    [dic setValue:fileName          forKey:@"MyFilename.jpg"];
//    [dic setValue:@"200"            forKey:@"success_action_status"];
    [manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imagedata name:fileName fileName:fileName mimeType:@"image/jpeg"];

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSLog(@"++++%ld",(long)response.statusCode);
    }];
}


展开
收起
迪迪控 2017-11-04 14:10:50 3085 0
1 条回答
写回答
取消 提交回答
  • 发送的host写对了吗?前面要加bucket的.
    2017-11-09 08:01:43
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载