file上传代码 ios_ios 上传图片file文件格式

一般我们都会base64上传图片,代码如下:

- (void)uploadPhotoToSrver:(UIImage *) image withPhotoID:(NSString *)photoID andPhotoType:(PhotoType)type andImageText:(NSString *)text {

static int upLoadCount = 0;

NSData *imageData = UIImageJPEGRepresentation(scaledImg, 0.6);

NSString *dataStr = [imageData base64EncodedStringWithOptions:0];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];

manager.requestSerializer = [AFHTTPRequestSerializer serializer];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

//参数

NSDictionary *parameters = @{@"UserID" : userInfo.UserID, @"imageDataStr": dataStr};

// url String

NSString *baseURL = [NSString stringWithFormat: @"%@%@", WEBBASEURL, WEBUploadPhotoDataAction];

[manager POST:baseURL parameters:parameters success:^(AFHTTPRequestOperation *operation,id responseObject) {

NSLog(@"上传成功");

}failure:^(AFHTTPRequestOperation *operation,NSError *error) {

MYNSLOG(@"~~~~~~~~%@",error);

}];

}

然后服务器要求file文件格式上传:

服务器要求需要传入的参数下图所示:

1.png

具体通过AFN实现代码如下所示:

NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1)];

ECGFeedbackInfo *info = [[ECGFeedbackInfo alloc]init];

info.imageData = imageData;

info.feedbackOpinion = @"bug-bug-bug";

info.phoneNum = @“1888888888”;

[[ECGpatientService sharedInstance] uploadFeedbckImageWithFeedbackInfo:info alertContent:alertContent success:^(id obj) {

NSLog(@"用户反馈意见上传成功");

successBlcok();

} fail:^{

NSLog(@"用户反馈意见上传失败");

}];

- (void)uploadFeedbckImageWithFeedbackInfo:(ECGFeedbackInfo *)info alertContent:(NSString *)content success:(SuccessBlockWithObject)success fail:(void (^)())fail {

NSString *url = @“你的URL地址”;

NSDictionary *parameter = [NSDictionary

dictionaryWithObjectsAndKeys:info.phoneNum,@"phone",info.feedbackOpinion,nil];

[ECGAFNetWorking uploadFeedbackImageWithURLString:url content:content parameters:parameter uploadDatas:info.imageData completeSuccess:^(NSDictionary *respinseDic, id responseObject) {

EcgResponseMessage *responseMessage = [[EcgResponseMessage alloc] initWithPropertyDictionary:respinseDic];

success(responseMessage);

} completeFailure:^{

fail();

}];

}

/**

上传图片

@param URLString URL

@param content 弹框的内容

@param parameters 参数体

@param uploadDatas 上传图片NSData

@param completeSuccess 成功回调

@param completeFailure 失败回调

*/

+ (void)uploadFeedbackImageWithURLString:(NSString *)URLString content:(NSString *)content parameters:(id)parameters uploadDatas:(NSData *)uploadImageData completeSuccess:(void (^)(NSDictionary *respinseDic, id responseObject))completeSuccess completeFailure:(void (^)())completeFailure{

NSString *tips = nil;

// 展示等待框

if ([Public isNum:content]) {

tips = LOAD_STRING(@"net_wait");

}else {

tips = content;

}

// 弹框

[ECGCustomAlertView showWaitPopViewWithContent:tips];

URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

AFHTTPSessionManager *manager = [AFHTTPSessionManager

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/html",nil];

// 设置超时时间

[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];

manager.requestSerializer.timeoutInterval = 20.f;

[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];

__weak typeof(self) weakSelf = self;

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

NSDateFormatter *formatter=[[NSDateFormatter alloc]init];

formatter.dateFormat=@"yyyyMMddHHmmss";

NSString *str=[formatter stringFromDate:[NSDate date]];

NSString *fileName=[NSString stringWithFormat:@"%@.jpg",str];

[formData appendPartWithFileData:uploadImageData name:@"content_pic" fileName:fileName mimeType:ECGBJKeyJPEG];

} progress:^(NSProgress * _Nonnull uploadProgress) {

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

// 转换responseObject对象

NSDictionary *dict = nil;

if ([responseObject isKindOfClass:[NSDictionary class]]) {

dict = (NSDictionary *)responseObject;

} else {

dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

}

// 成功后弹框处理,回调出去

[weakSelf notDismissedWith:dict responseObject:responseObject complete:^(NSDictionary *respinseDic, id responseObject) {

completeSuccess(respinseDic,responseObject);

} failedComplete:^{

completeFailure();

}];

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

// 网络问题,弹框处理

[weakSelf notNetconnetNotDismissComplete:^{

completeFailure();

}];

}];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值