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


这问题困扰我好长时间,不上传文件就不是乱码,传文件就乱码,并且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了吧?  .算了 凑合看吧!!!!

本文出自 “劣徒_不学无术” 博客,请务必保留此出处http://lietu.blog.51cto.com/6212247/1562597

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。