这问题困扰我好长时间,不上传文件就不是乱码,传文件就乱码,并且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了吧? .算了 凑合看吧!!!!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享名称:解决ASIHTTPsetFile上传文件后中文乱码问题-创新互联
转载来于:/article14/dojige.html
成都网站建设公司_创新互联,为您提供做网站、静态网站、品牌网站制作、响应式网站、品牌网站建设、网站设计公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联