in Education by
in my app i want to upload an image chosen via UIImagePickerController to a database which only accepts JPEG images. I've browsed many questions in here, and in other forums but i still didn't get it to work. I hope you can check my code, if there is a mistake which i can't see. This is the complete method for uploading, with an image description, an image title, and the geodata for this image: - (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]; } The GEOimage chosenImage is created via the CGImageRef after an UIImage is chosen in the ImagePickerController. Method nr. *1 to get the NSData for upload is not the best, because here the image is chosen from the document directory, and here every EXIF information about the image is deleted. With both methods i get the response from the database that the image filetype is not supported (JPEG expected). I thought with method nr. *2 i'm sending an JPEG image, but perhaps i have a mistake in the whole multipart/formdata process. I tried to get the URL to the original file on the filesystem, but this is quite difficult for me. I only got the assets-url from the image. Thanks in advance S4lfish JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You've probably solved it on your own by now, but I think I see your problem. NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; // header value, user session ID added NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", sessionID]; When you're defining the boundary inside the MIME header, you're using your sessionID as the boundary. Then, down below, you're using your stringBoundary variable to create the actual boundaries. [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; It might also be a good idea to provide the Content-Length of each MIME block to help the processing code, but I don't think that's at issue here.

Related questions

0 votes
    I'm developing an Android app and I'm working on photo uploading to my rails server. If I upload ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm building a single page application. On the client side I'm using Nuxt.js (which includes Vue. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am using Laravel Image Intervention to resize an image upload field on my form. This is the error ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Currently I'm using Cloudinary.com to save my users images. I know how to do some of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I've followed all guides and answers, and everything displays correctly, but the actual upload doesn't ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    I've followed all guides and answers, and everything displays correctly, but the actual upload doesn't ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    I have a form with a File Input and one Button, when I press the button the file should go to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    When you upload a CSV file through UI or External Data API, it is recommended to provide the metadata in the format of __________. A. CSV B. JSON C. XML...
asked Nov 1, 2022 in Education by JackTerrance
0 votes
    I have made a chatroom where users can messages to each other but I want to add a upload file ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    On Internet Explorer, the standard HTML file upload form also allows for direct input of the file name ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I writing a function in my form that require user to upload a file, but doesn't necessary have to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    So I'm using Codeigniter and I want to upload files. It's working fine except for this one thing. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I want to upload the file pdf to the folder but a file is not saved to a folder. I already read the ... .run(debug=True)``` Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    To lower the file size, while uploading, in Scratch, check the _________ box in the left corner of the upload screen Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    How to upload a file in Django?...
asked Feb 1, 2021 in Technology by JackTerrance
...