objective-c – 使用http POST上传多个文件
发布时间:2020-12-16 03:39:56 所属栏目:百科 来源:网络整理
导读:我在从iPhone应用程序上使用FTP上传文件时遇到问题. 我正在向我的webservice发送一个带有所有必要参数的HTTP POST请求.我发送两个文件,一个是图像,第二个是音频.图像正在上传,但音频不上传. 当我跟踪我的Web服务时,它只显示图像字段作为上传文件.它没有表明
我在从iPhone应用程序上使用FTP上传文件时遇到问题.
我正在向我的webservice发送一个带有所有必要参数的HTTP POST请求.我发送两个文件,一个是图像,第二个是音频.图像正在上传,但音频不上传. 我的代码是: NSString *urlString = [NSString stringWithFormat:ADD_LEAD_API_URL]; NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; // change type to POST (default is GET) [postRequest setHTTPMethod:@"POST"]; // just some random text that will never occur in the body NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; // header value NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; // set header [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; // create data NSMutableData *postBody = [NSMutableData data]; [postBody appendData:[[NSString stringWithFormat:@"--%@rn",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userId"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[appDelegate.objCurrentUser.userId dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]]; // message part [postBody appendData:[[NSString stringWithFormat:@"--%@rn",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="firstName"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[firstName dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]]; /*** My rest of string parameters are successfully added to request ***/ // media part [postBody appendData:[[NSString stringWithFormat:@"--%@rn",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="imageUrl"; filename="dummy.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Type: image/jpegrn" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Transfer-Encoding: binaryrnrn" dataUsingEncoding:NSUTF8StringEncoding]]; // get the image data from main bundle directly into NSData object UIImage *img= leadImage; NSData *imageData= UIImageJPEGRepresentation(img,90); [postBody appendData:imageData]; // Image is being uploaded [postBody appendData:[[NSString stringWithFormat:@"--%@rn",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="recordSoundUrl"; filename="%@"rn",self.recordingPath] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Type: application/octet-streamrn" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Transfer-Encoding: binaryrnrn" dataUsingEncoding:NSUTF8StringEncoding]]; NSData *soundData = [NSData dataWithContentsOfFile:[appDelegate.documentDir stringByAppendingPathComponent:self.recordingPath]]; [postBody appendData:soundData]; [postBody appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]]; // final boundary [postBody appendData:[[NSString stringWithFormat:@"--%@rn",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; // add body to post [postRequest setHTTPBody:postBody]; // Asynch request NSURLConnection *conn = [NSURLConnection connectionWithRequest:postRequest delegate:self]; 如果我不上传图像,则需要音频.所以我只能发送一个带有请求的文件. 提前致谢. 解决方法
你有没有看过使用
ASIHTTPRequest的ASINetworkQueue发送多个文件.
更新:根据下面的评论,不再维护ASIHTTPRequest.请谨慎使用此框架.其他选项是MKNetworkKit或AFNetworking. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |