AFN文件上传appendPartWithFileData:<#(nonnull NSData *)#> name:<#(nonnull NSString *)#>

    xiaoxiao2024-12-30  29

    Demo---

    /**

     *   //网络的管理者

     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

     

     //地址

     NSString *urlstr = @"http://localhost/post/upload-m.php";

     

     NSDictionary *dic = @{@"status":@"使用第三方上传真爽"};

     //发送请求

     [manager POST:urlstr parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

     //文件1

     NSString *filePath = [[NSBundle mainBundle]pathForResource:@"sunli.jpg" ofType:nil];

     NSData *fileData = [NSData dataWithContentsOfFile:filePath];

     //文件2

     NSString *filePath2 = [[NSBundle mainBundle]pathForResource:@"car.jpg" ofType:nil];

     NSData *fileData2 = [NSData dataWithContentsOfFile:filePath2];

     

     [formData appendPartWithFileData:fileData name:@"userfile[]" fileName:@"dengchao.jpg" mimeType:@"image/jpg"];

     

     [formData appendPartWithFileData:fileData2 name:@"userfile[]" fileName:@"makalun.jpg" mimeType:@"image/jpg"];

     

     } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

     NSLog(@"responseObject %@",responseObject);

     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

     NSLog(@"error %@",error);

     }];

     [formData appendPartWithFileData:fileData name:@"userfile[]" fileName:@"dengchao.jpg" mimeType:@"image/jpg"];

    执行这个方法时, name:部分是服务器用来解析的字段, 而fileName则是直接上传上去的图片, 注意一定要加 .jpg或者.png,(这个根据你得到这个imgData是通过jepg还是png的方式来获取决定)。 然后mimeType值也要与上面的类型对应, 网上看到有的说直接写成 @"image/*", 据说也是可以的, 没验证过。 
    转载请注明原文地址: https://ju.6miu.com/read-1295148.html
    最新回复(0)