【IOS学习之路】关于GPRS下上传文件,ASIFormDataRequest在2G网络下上传失败(已解决)iphone开发
这几天真是把我算是认清楚了ASIHTTPRequest,我的客户端上需要上传一个文件到我的服务器上,在不是中国移动的网络中上传文件使用ASIHTTPRequest是一点没有问题的,但是要是遇到了中国移动,我不得不说,fuck
下面说解决的代码
[self setRequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:UPLOADFILE_ADDRESS]]]; NSData *data = [NSData dataWithContentsOfFile:self.filepath]; NSLog(@"crystr = %@",_crystr); NSLog(@"userid = %@",_userinfo.userId); [_request setPostValue:_userinfo.userId forKey:@"userID"]; [_request setPostValue:@"14" forKey:@"uploadTypeID"]; [_request setPostValue:@"11" forKey:@"Applicationid"]; [_request setPostValue:_crystr forKey:@"crystr"]; [_request addData:data withFileName:self.filepath andContentType:@"image/png" forKey:@"FileData"]; [_request setDelegate:self]; [_request setUploadProgressDelegate:self]; _request.showAccurateProgress = YES; [_request setTimeOutSeconds:30]; [_request startAsynchronous]; [_request setTag:UPLOAD_FILE];
上面这是我使用ASIHTTPRequest写的方式,要是遇到iphone客户端还是绕了吧,估计你会头痛,换用下面的方式,
首先看下我服务端上传数据时候回使用的一些数据,根据这些数据然后配置自己网络请求需要使用到的一些上传方式
Content-Type: multipart/form-data; boundary=----------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0
Content-Disposition: form-data; name="userid" 12312 ------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0 Content-Disposition: form-data; name="id" 198653687 ------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0 Content-Disposition: form-data; name="applicationid" 11 ------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0 Content-Disposition: form-data; name="crystr" 70C0C406225F2545F37BD231BBDCA780 ------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0 Content-Disposition: form-data; name="uploadTypeID" 14 ------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0 Content-Disposition: form-data; name="Filedata"; filename="ss.jpg" Content-Type: application/octet-stream ------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0 Content-Disposition: form-data; name="Upload" Submit Query ------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0 --------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0 Content-Disposition: form-data; name="Filename" ss.jpg ------------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0
下面写上在在c#中我是怎么上传图片文件的方式
WebRequest req = WebRequest.Create("http://localhost:802/uploadimg.aspx"); req.ContentType = "multipart/form-data; boundary=" + boundary; req.Method = "POST"; //组织表单数据 StringBuilder sb = new StringBuilder(); foreach (KeyValuePair<string, string> dicItem in inputDic) { sb.Append("--" + boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"" + dicItem.Key + "\""); sb.Append("\r\n"); sb.Append("\r\n"); sb.Append(dicItem.Value);/alue前面必须有2个换行 sb.Append("\r\n"); } //拼接文件控件 sb.Append("--" + boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"Filedata\"; filename=\"1e4c9eb5a6a1059f37d3ca70[1].jpg\""); sb.Append("\r\n"); sb.Append("Content-Type: application/octet-stream"); sb.Append("\r\n"); sb.Append("\r\n"); string head = sb.ToString(); byte[] form_data = Encoding.UTF8.GetBytes(head); //结尾 byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); //文件 FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); //post总长度 long length = form_data.Length + fileStream.Length + foot_data.Length; req.ContentLength = length; Stream requestStream = req.GetRequestStream(); //发送表单参数 requestStream.Write(form_data, 0, form_data.Length); //文件内容 byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) requestStream.Write(buffer, 0, bytesRead); //结尾 requestStream.Write(foot_data, 0, foot_data.Length); requestStream.Close();
参考上面的方式修改代码就有了O-C上传文件的方式
//服务器的路径 NSData *data = [[[NSMutableData alloc] initWithLength:256*1024] autorelease]; NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"file.jpg"]; UIImage *img = [UIImage imageNamed:@"2.jpg"]; [UIImageJPEGRepresentation(img, 1) writeToFile:filePath atomically:NO]; //NSString *filePath=upLoadFilePath.text; NSData *fileData =[[NSData alloc] initWithContentsOfFile:filePath]; //NSLog(@"fileData = %@",fileData); //压缩数据,检查上传文件后发现乱码,所以应用时得解码 //NSData *fileData=[self compress:Data]; //64位数据编码,不知如何测试其编码是否成功 //NSString *data=[UploadAndDownloadViewController base64forData:fileData]; //获取当前路径下文件名,为上传同名做准备 NSFileManager *fileManager = [[NSFileManager alloc] init]; NSString *displayNameAtPath = [fileManager displayNameAtPath:filePath]; //将要上传文件路径 NSString *urlString =@"上传文件的地址"; //文件传输请求 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; //迷惑 NSString *boundary = [NSString stringWithString:@"----------ae0GI3Ef1Ef1Ij5GI3KM7ae0Ij5ae0"]; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; //数据传输格式,组合http传输的,数据包头、数据、包尾 NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userID\"\r\n\r\n%@\r\n",@"39562858"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadTypeID\"\r\n\r\n%@\r\n",@"14"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Applicationid\"\r\n\r\n%@\r\n",@"11"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"crystr\"\r\n\r\n%@\r\n",@"07416F9D3B9C9899221C87B88DB93B4B"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Filedata\"; filename=\"%@\"\r\n",filePath] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:fileData]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; NSString *str = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]; NSLog(@"str = %@", str); //请求同步加载 //NSURLResponse *urlResponse; //[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:nil]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; } - (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData { NSString *str = [[NSString alloc] initWithData:incrementalData encoding:NSUTF8StringEncoding]; NSLog(@"str = %@", str); }
OK