gpt4 book ai didi

objective-c - 如何在不使用 ASIHTTPRequest 的情况下上传多张图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:27 27 4
gpt4 key购买 nike

我想用下面的代码将两张图片上传到服务器,谁能帮帮我,这是我的发布数据代码

- (void)uploadPath:(NSString*)path withOptions:(NSDictionary*)options withImageData1:(NSData*)data1 ofImageName1:(NSString *)imageName1 andImageData2:(NSData*)data2 ofImageName2:(NSString *)imageName2 {  
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:baseURLString]];
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:90.0];

NSString *boundary = @"-------1234567";

[mutableRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
[mutableRequest setHTTPMethod:@"POST"];
NSMutableData *postbody = [NSMutableData data];

for(NSString *key in options)
{
NSString *value = [options objectForKey:key];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//******************* Append 1st Image ************************/

if ([imageName1 isEqualToString:@"picture1.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture1\"; filename=\"%@\"\r\n",imageName1] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data1]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/

//******************* Append 2nd Image ************************/

if ([imageName2 isEqualToString:@"picture2.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture2\"; filename=\"%@\"\r\n",imageName2] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data2]];

//******************* Append 2nd Image ************************/

[mutableRequest setHTTPBody:postbody];
urlConnection = [[NSURLConnection alloc] initWithRequest:mutableRequest delegate:self];
if (self.urlConnection) {
self.dataXml = [NSMutableData data];
}
self.urlConnection = nil;
[mutableRequest release];
[pool release];
}

最佳答案

查看错误的一种解决方案:

  1. 创建一个新项目,使用 ASIHttpRequest
  2. 获取网络调试代理,如 Charles
  3. 打开 Charles(或其他),进行成功的多文件上传。记录从 HTTP 流量到服务器的数据。
  4. 现在运行您自己的代码。记录该流量。有什么不同你的流量到工作流量?让你的完全相同尊重,它怎么行不通?

您还可以将步骤 (1) 替换为 curl 命令行,该命令行将多个文件上传到服务器并记录该流量,但我对 curl 还不够熟悉> 凭空说出该命令的样子。

关于objective-c - 如何在不使用 ASIHTTPRequest 的情况下上传多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9123728/

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