189 8069 5689

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

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

创新互联公司是一家以成都网站建设、网页设计、品牌设计、软件运维、成都网站营销、小程序App开发等移动开发为一体互联网公司。已累计为成都纸箱等众行业中小客户提供优质的互联网建站和软件开发服务。


--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"];#endifNSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([selfstringEncoding]));// 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 = [NSStringstringWithFormat:@"0xKhTmLbOuNdArY-%@",uuidString];[selfaddRequestHeader:@"Content-Type"value:[NSStringstringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary]]; [selfappendPostString:[NSStringstringWithFormat:@"--%@\r\n",stringBoundary]];// Adds post dataNSString *endItemBoundary = [NSStringstringWithFormat:@"\r\n--%@\r\n",stringBoundary];NSUInteger i=0;for (NSDictionary *valin [selfpostData]) { [selfappendPostString:[NSStringstringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\nContent-Type:text/plain; charset=UTF-8\r\n\r\n",[valobjectForKey:@"key"]]]; [selfappendPostString:[valobjectForKey:@"value"]]; i++;if (i != [[selfpostData]count] || [[selffileData]count] >0) {//Only add the boundary if this is not the last item in the post body [selfappendPostString:endItemBoundary]; } }// Adds files to upload i=0;for (NSDictionary *valin [selffileData]) { [selfappendPostString:[NSStringstringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", [valobjectForKey:@"key"], [valobjectForKey:@"fileName"]]]; [selfappendPostString:[NSStringstringWithFormat:@"Content-Type: %@\r\n\r\n", [valobjectForKey:@"contentType"]]];id data = [valobjectForKey:@"data"];if ([dataisKindOfClass:[NSStringclass]]) { [selfappendPostDataFromFile:data]; }else { [selfappendPostData:data]; } i++;// Only add the boundary if this is not the last item in the post bodyif (i != [[selffileData]count]) { [selfappendPostString:endItemBoundary]; } } [selfappendPostString:[NSStringstringWithFormat:@"\r\n--%@--\r\n",stringBoundary]];#if DEBUG_FORM_DATA_REQUEST[self addToDebugBody:@"==== End of multipart/form-data body ====\r\n"];#endif}

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


当前标题:解决ASIHTTPsetFile上传文件后中文乱码问题
文章出自:http://jkwzsj.com/article/pgeesg.html

其他资讯