gpt4 book ai didi

objective-c - 使用 http POST 上传多个文件

转载 作者:太空狗 更新时间:2023-10-30 03:42:04 26 4
gpt4 key购买 nike

我在从我的 iPhone 应用程序使用 FTP 上传文件时遇到问题。

我正在向我的网络服务发送一个包含所有必要参数的 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:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[appDelegate.objCurrentUser.userId dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


// message part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"firstName\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[firstName dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

/*** My rest of string parameters are successfully added to request ***/


// media part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imageUrl\"; filename=\"dummy.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" 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:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"recordSoundUrl\"; filename=\"%@\"\r\n", self.recordingPath] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: application/octet-stream\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


NSData *soundData = [NSData dataWithContentsOfFile:[appDelegate.documentDir stringByAppendingPathComponent:self.recordingPath]];
[postBody appendData:soundData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

// add body to post
[postRequest setHTTPBody:postBody];

// Asynch request
NSURLConnection *conn = [NSURLConnection connectionWithRequest:postRequest delegate:self];

如果我不上传图片,它就会上传音频。所以我只能随请求发送一个文件。请帮助我,如果我哪里错了,请告诉我。

提前致谢。

最佳答案

其实很简单。只是形成多部分的 body 。假设您有要上传的文件和“params”字典中的常规参数:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: urlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:54.0];

NSMutableData* body = [NSMutableData data];

NSString* boundary = [NSString randomStringWithLength:64];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
NSData *boundaryData = [[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding];

[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[body appendData:boundaryData];
// I use special simple class to distinguish file params
if( [obj isKindOfClass:[FileUpload class]] ) {
// File upload
[body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", key, [obj localName]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData: [obj loadData]]; // It just return NSData with loaded file in it
[body appendData: [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
else {
// Regular param
[body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n", key, obj] dataUsingEncoding:NSUTF8StringEncoding]]
}
}];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:body];

现在你可以用任何方式发送它:

[NSURLConnection sendAsynchronousRequest:request
queue:queueOfYourChoice
completionHandler:^(NSURLResponse *response, NSData *rdata, NSError *error)

FileUpload 类非常简单:它从 url/文件名构造,提供此名称作为方法并将二进制内容加载到 NSData* 中。为了清楚起见,我没有列出它,但如果需要,我会添加。

关于objective-c - 使用 http POST 上传多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5716645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com