将图像上传到服务器详细说明初学者

我正在努力将图像上传到服务器上两天,因为有很多关于通过AFNetworking和NSURLSession上传图像的问题以及其他上传所有我想问的方法是我没有找到一个答案来解释关于事情是如何工作以及在幕后发生了什么的整个概念我在搜索youtube时所有的东西都可以在Swift中找到并且完全不相信我的结果我发现这个答案对我来说很熟悉



//Init the NSURLSession with a configuration NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; //Create an URLRequest NSURL *url = [NSURL URLWithString:@"yourURL"]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; //Create POST Params and add it to HTTPBody NSString *params = @"api_key=APIKEY&email=example@example.com&password=password"; [urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; //Create task NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //Handle your response here }]; [dataTask resume]; 


关于这个话题最受欢迎的答案是用户XJones : –

 Here's code from my app to post an image to our web server: // Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept. NSMutableDictionary* _params = [[NSMutableDictionary alloc] init]; [_params setObject:[NSString stringWithString:@"1.0"] forKey:[NSString stringWithString:@"ver"]]; [_params setObject:[NSString stringWithString:@"en"] forKey:[NSString stringWithString:@"lan"]]; [_params setObject:[NSString stringWithFormat:@"%d", userId] forKey:[NSString stringWithString:@"userId"]]; [_params setObject:[NSString stringWithFormat:@"%@",title] forKey:[NSString stringWithString:@"title"]]; // the boundary string : a random string, that will not repeat in post data, to separate post data fields. NSString *BoundaryConstant = [NSString stringWithString:@"----------V2ymHFg03ehbqgZCaKO6jy"]; // string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ NSString* FileParamConstant = [NSString stringWithString:@"file"]; // the server url to which the image (or the media) is uploaded. Use your server url here NSURL* requestURL = [NSURL URLWithString:@""]; // create request NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setHTTPShouldHandleCookies:NO]; [request setTimeoutInterval:30]; [request setHTTPMethod:@"POST"]; // set Content-Type in HTTP header NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant]; [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; // post body NSMutableData *body = [NSMutableData data]; // add params (all params are strings) for (NSString *param in _params) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; } // add image data NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0); if (imageData) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:imageData]; [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; } [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; // setting the body of the post to the reqeust [request setHTTPBody:body]; // set the content-length NSString *postLength = [NSString stringWithFormat:@"%d", [body length]]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; // set URL [request setURL:requestURL]; 


但我的观点是我正在自学,对于没有解释的初学者来说很难理解所以我所要求的只是一个解释,一个关于整个过程的细节解释如果有人很难花在这上面问题,因为不管你信不信,我发现这是迄今为止最困难的话题,因为主要原因是没有关于整个过程的教程,如果有人能够现在迈出一步并解释这个概念,那么对初学者也没有任何解释对将要学习明天的学生。 因此,任何能够详细解释这一点的人以及上传过程如何运作以及参考的一些步骤将不胜感激。

注意 :请考虑我有API和密钥“图像”。

这里我们将看一下图像上传以及一些**参数,因为大多数时候我们上传图像以及一些参数,例如userId。

  • 在深入讨论我们的主题之前,让我提供完成源代码的代码。我们将在下面看到的所有细节来自其他一些堆栈溢出线程和其他站点的一些,我将提供所有链接供您参考。

     -(void)callApiWithParameters:(NSDictionary *)inputParameter images:(NSArray *)image imageParamters:(NSArray *)FileParamConstant{ //1 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setHTTPShouldHandleCookies:NO]; [request setTimeoutInterval:30]; [request setHTTPMethod:@"POST"]; //2 NSString *boundary = @"------CLABoundaryGOKUL"; //3 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; //4 NSMutableData *body = [NSMutableData data]; for (NSString *key in inputParameter) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", [inputParameter objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]]; } for (int i = 0; i < image.count; i++) { NSData *imageDatasss = UIImagePNGRepresentation(image[i]); if (imageDatasss) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant[i]] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:imageDatasss]; [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; } } [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; //5 [request setHTTPBody:body]; //6 [request setURL:[NSURL URLWithString:@"http://changeThisWithYourbaseURL?"]];//Eg:@"http://dev1.com/PTA_dev/webservice/webservice.php?" //7 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; //8 if ([httpResponse statusCode] == 200) { NSDictionary * APIResult =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"Response of %@: %@",[inputParameter valueForKey:@"service"],APIResult); }else{ //9 NSLog(@"%@",error.localizedDescription); } }]; } 

    注意:由于这是一个广泛的主题,我提供了详细信息的文档链接。

    1. 我们正在使用** NSMutableURLRequest **而不是** NSURLRequest **,因为我们要向它附加一些数据。如果您需要对可变URL请求进行深入澄清,请阅读本文档 。
      • setHTTPShouldHandleCookies在这里我们必须决定是否要使用cookies。要了解更多有关访问的信息
      • setTimeoutInterval这有助于为url request设置时间限制。在给定时间后添加时间间隔(以秒为单位),请求将被终止。
      • setHTTPMethod有很多方法。但是在很多情况下我们使用GETPOST方法.POST和GET之间的差异在这里和这里
    2. 边界有助于将参数彼此分离,以便服务器可以识别它们。边界可以是您希望随意编辑它的任何内容。
    3. 这里我们使用multipart / form-data; boundary = as content type。要知道我们为什么要使用这个内容类型来查看这个post。
    4. NSMutableData * body我们要将所有参数和值附加到此数据,然后将setHTTPBody添加UrlRequest

      • 如果这是我们称之为'callApiWithParameters'的方法

          - (IBAction)Done:(id)sender{ NSDictionary * inputParameters = [NSDictionary dictionaryWithObjectsAndKeys: @"1",@"user_id" , "XXX",@"name" , nil]; NSArray * image = [NSArray arrayWithObjects:[UIImage imageNamed:@"Test"],[UIImage imageNamed:@"Test1"],nil]; NSArray * imageParameters = [NSArray arrayWithObjects:@"img_one",@"img_two",nil]; [self callApiWithParameters:inputParameters images:image imageParamters:imageParameters]; } 
      • 然后数据(即正文)将如下所示

 Content-Type=multipart/form-data; boundary=------CLABoundaryGOKUL --------CLABoundaryGOKUL Content-Disposition: form-data; name=user_id 1 --------CLABoundaryGOKUL Content-Disposition: form-data; name=name XXX --------CLABoundaryGOKUL Content-Disposition: form-data; name=img_one; filename=image.jpg Content-Type:image/jpeg //First image data appended here --------CLABoundaryGOKUL Content-Disposition: form-data; name=img_two; filename=image.jpg Content-Type:image/jpeg //Second image data appended here. 
  • 上面给出的数据将清楚地解释发生了什么,所有参数和键都已添加到数据中。 在这里,您可以找到有关发送multipart / form的更多详细信息。

    1. 现在只需通过[request setHTTPBody:body];添加上述数据来请求[request setHTTPBody:body];
    2. 此方法中的setURL会添加您应用的基本url。
    3. 现在我们需要做的就是建立与服务器的连接并发送请求。我们使用NSURLConnection发送请求。 有关NSURLConnection的说明加载URL请求的数据,并在请求完成或失败时在操作队列上执行处理程序块。
    4. statusCode有助于确定我们是否从服务器获得了成功的响应。 如果200表示正常,则500表示内部服务器错误等。 此处提供更多详细信息。

    5. 处理其他情况下的错误。

仅供参考我已经解释了我能做什么,请参考链接以便更好地理解。

编辑:

只需更改imageParamater数组中的名称,以满足您的需求使用image更改img_one&img_two

  - (IBAction)Done:(id)sender{ //Change input parameters as per your requirement. NSDictionary * inputParameters = [NSDictionary dictionaryWithObjectsAndKeys: @"1",@"user_id" , "XXX",@"name" , nil]; NSArray * image = [NSArray arrayWithObjects:[UIImage imageNamed:@"Test"],nil]; //Change Test with your image name NSArray * imageParameters = [NSArray arrayWithObjects:@"image",nil];//Added image as a key. [self callApiWithParameters:inputParameters images:image imageParamters:imageParameters]; } 

并使用示例基本URL更改Point 6,

// 6

  [request setURL:[NSURL URLWithString:@"http://google.com/files/upload.php?"]]; 

我觉得它对你有帮助……

 - (void)sendImageToServer { UIImage *yourImage= [UIImage imageNamed:@"image.png"]; NSData *imageData = UIImagePNGRepresentation(yourImage); NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; NSString *strImage = [NSString stringWithFormat:@"data:image/png;base64,%@",base64]; NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:strImage,@"image", nil]; NSError * err; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&err]; NSString *UserProfileInRequest = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSData *data=[UserProfileInRequest dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *len = [NSString stringWithFormat:@"%ld", (unsigned long)[data length]]; // Init the URLRequest NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init]; [req setURL:[NSURL URLWithString:@"http://YOUR_URL"]]; [req setHTTPMethod:@"POST"]; [req setValue:len forHTTPHeaderField:@"Content-Type"]; [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [req setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [req setHTTPBody:data]; NSURLSession *session = [NSURLSession sharedSession]; [[session dataTaskWithRequest:req completionHandler:^(NSData *dt, NSURLResponse *response, NSError *err){ //Response Data NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:dt options:kNilOptions error:&err]; NSLog(@"%@", [dic description]); }]resume]; } 

使用AFNetworking为此任务提供非常简单可靠的解决方案。