温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

解决ASIHTTP setFile上传文件后中文乱码问题

发布时间:2020-07-19 23:51:15 来源:网络 阅读:819 作者:yvbang 栏目:移动开发


这问题困扰我好长时间,不上传文件就不是乱码,传文件就乱码,并且Android好使;
最后联合中间件后台/Android客户端/IOS客户端,共同上传比较,
最后在后台捕捉时发现,Android的提交输出格式为:


--PfyXAYcEcmd3GqueWEk6hXUWXfm-KrG4XNEQContent-Disposition: form-data; name="companyName"Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit


公司名


IOS的提交输出格式为:
--0xKhTmLbOuNdArY-C14CFE75-F1D5-4E68-BFDB-93957E2DDCEFContent-Disposition: form-data; name="companyName"


公司名


发现post的Value都是中文的,都没有经过编码, 不过Android的设置了Content-Type, 而IOS的ASIHttpRequest中,上传文件时忽略了Content-Type的头注入,导致后台不识别编码模式, 不过应该是后台特意对Android做过些处理, (之前Android传文件是乱码).现在问题解决了!!

下面是代码修改处:


在ASIFormDataRequest 中的 - (void)buildMultipartFormDataPostBody 方法修改

高亮处为修改代码:



- (void)buildMultipartFormDataPostBody { #if DEBUG_FORM_DATA_REQUEST [self addToDebugBody:@"\r\n==== Building a multipart/form-data body ====\r\n"]; #endif NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); // We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. CFUUIDRef uuid = CFUUIDCreate(nil); NSString *uuidString = [(NSString*)CFUUIDCreateString(nil, uuid) autorelease]; CFRelease(uuid); NSString *stringBoundary = [NSString stringWithFormat:@"0xKhTmLbOuNdArY-%@",uuidString]; [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary]]; [self appendPostString:[NSString stringWithFormat:@"--%@\r\n",stringBoundary]]; // Adds post data NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary]; NSUInteger i=0; for (NSDictionary *val in [self postData]) { [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\nContent-Type:text/plain; charset=UTF-8\r\n\r\n",[val objectForKey:@"key"]]]; [self appendPostString:[val objectForKey:@"value"]]; i++; if (i != [[self postData] count] || [[self fileData] count] > 0) { //Only add the boundary if this is not the last item in the post body [self appendPostString:endItemBoundary]; } } // Adds files to upload i=0; for (NSDictionary *val in [self fileData]) { [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", [val objectForKey:@"key"], [val objectForKey:@"fileName"]]]; [self appendPostString:[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", [val objectForKey:@"contentType"]]]; id data = [val objectForKey:@"data"]; if ([data isKindOfClass:[NSString class]]) { [self appendPostDataFromFile:data]; } else { [self appendPostData:data]; } i++; // Only add the boundary if this is not the last item in the post body if (i != [[self fileData] count]) { [self appendPostString:endItemBoundary]; } } [self appendPostString:[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary]]; #if DEBUG_FORM_DATA_REQUEST [self addToDebugBody:@"==== End of multipart/form-data body ====\r\n"]; #endif }





**** 51cto  .富文本代码语言竟然没有ObjC的????   这也太low了吧?  .算了 凑合看吧!!!!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI