Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Multipart/Formdata图像上传,获取文件_Ios_Image_Upload_Multipart - Fatal编程技术网

Ios Multipart/Formdata图像上传,获取文件

Ios Multipart/Formdata图像上传,获取文件,ios,image,upload,multipart,Ios,Image,Upload,Multipart,在我的应用程序中,我想将通过UIImagePickerController选择的图像上传到只接受JPEG图像的数据库。我在这里和其他论坛上浏览了很多问题,但我仍然没有让它发挥作用。我希望你能检查我的代码,如果有我看不到的错误。 这是上载此图像的完整方法,包括图像描述、图像标题和地理数据: - (void) uploadPic{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUser

在我的应用程序中,我想将通过
UIImagePickerController
选择的图像上传到只接受JPEG图像的数据库。我在这里和其他论坛上浏览了很多问题,但我仍然没有让它发挥作用。我希望你能检查我的代码,如果有我看不到的错误。 这是上载此图像的完整方法,包括图像描述、图像标题和地理数据:

- (void) uploadPic{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *destinationFilePath = [[NSString alloc] initWithFormat: @"%@/%@.jpeg", documentsDirectory, self.chosenImage.imgTitle];
    NSURL *fileURL = [[NSURL alloc]initWithString:destinationFilePath];
    NSLog(@"will upload file: %@", destinationFilePath);

    NSData *imageURLdata = [[NSData alloc]initWithContentsOfURL:fileURL];   (*1) 
    NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 90);    (*2) 
    //Here i tried 2 ways of getting the data for uploading, but both don't work.

    // create the URL
    NSURL *postURL = [NSURL URLWithString:@"http://*********/PictureUpload"];

    // create the connection
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];

    // 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, user session ID added
    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
                                sessionID];

    // set header
    [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
    // create data
    NSMutableData *postBody = [NSMutableData data];

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

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


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

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

    // media part
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.jpeg\"\r\n", self.chosenImage.imgTitle ] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[NSData dataWithData:imageURLdata]];
    [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    // final boundary
    [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *s = [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding];
    NSLog(@"%@", s);

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

    // pointers to some necessary objects
    NSURLResponse* response;
    NSError* error;

    // synchronous filling of data from HTTP POST response
    NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];

    if (error)
    {
        NSLog(@"Error: %@", [error localizedDescription]);
    }

    // convert data into string
    NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes]
                                                        length:[responseData length]
                                                      encoding:NSUTF8StringEncoding];

    // see if we get a welcome result
    NSLog(@"%@", responseString);
    [self responseHandler:responseString];

}
ImagePickerController
中选择
UIImage
后,通过
CGImageRef
创建
GEOimage
chosenImage
。 获取上传的
NSData
的方法nr.*1不是最好的方法,因为这里的图像是从文档目录中选择的,并且这里删除了关于图像的所有EXIF信息。 使用这两种方法,我从数据库得到的响应是不支持图像文件类型(预期为JPEG)。 我以为使用方法nr.*2发送的是JPEG图像,但可能在整个多部分/formdata过程中出错了

我试图获取文件系统上原始文件的URL,但这对我来说相当困难。我只从图像中获取了资源url

提前谢谢


S4lfish

到现在为止,你可能已经自己解决了这个问题,但我想我看到了你的问题

NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";

// header value, user session ID added
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
                            sessionID];
在MIME头中定义边界时,使用sessionID作为边界。然后,在下面,您使用
stringBoundary
变量来创建实际的边界

[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
提供每个MIME块的内容长度以帮助处理代码可能也是一个好主意,但我认为这不是问题所在。

//试试这个-->>

   NSMutableDictionary *post_dic=[[NSMutableDictionary
   alloc]initWithCapacity:20]; [post_dic setObject:firstName_txtFld.text
   forKey:@"firstname"]; [post_dic setObject:lastName_txtFld.text
   forKey:@"lastname"]; [post_dic setObject:email_txtFld.text
   forKey:@"email"]; [post_dic setObject:password_txtFld.text
   forKey:@"password"]; [post_dic setObject:country_txtFld.text
   forKey:@"address"]; [post_dic setObject:state_txtFld.text
   forKey:@"state"]; [post_dic setObject:city_txtFld.text
   forKey:@"city"]; [post_dic setObject:zip_txtFld.text forKey:@"zip"];
   [post_dic setObject:phoneNumber_txtFld.text forKey:@"phonenumber"];   
   NSURL *url = [NSURL URLWithString:@"Enter your url"];
   NSMutableURLRequest *urlRequest = [NSMutableURLRequest
   requestWithURL:url]; [urlRequest setHTTPMethod:@"POST"]; [urlRequest
   setHTTPBody:[urlString dataUsingEncoding:NSUTF8StringEncoding]];
            NSString *boundary = @"--------------------------14737809831466499882746641449"; NSString
   *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
   [urlRequest addValue:contentType
   forHTTPHeaderField: @"Content-Type"];
          NSMutableData *postbody = [NSMutableData data];
          for (NSString *param in post_dic) {
        [postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary]
  dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data;
   name=\"%@\"\r\n\r\n", param]
   dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[NSString stringWithFormat:@"%@\r\n", [post_dic objectForKey:param]]
   dataUsingEncoding:NSUTF8StringEncoding]];
            }
       [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]
   dataUsingEncoding:NSUTF8StringEncoding]];
       [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data;
   name=\"filedata\";
   filename=\"%@.jpg\"\r\n",@"text"]
   dataUsingEncoding:NSUTF8StringEncoding]];
       [postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n"
   dataUsingEncoding:NSUTF8StringEncoding]];
       //NSLog(@" image -->%@",imageData);
       [postbody appendData:[NSData dataWithData:imageData]];
       [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]
   dataUsingEncoding:NSUTF8StringEncoding]];
        [urlRequest setHTTPBody:postbody]; [urlRequest setURL:url]; 
        NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:urlRequest  delegate:self]; 
[connection start];