0

I am struggling to get the file path from imagePicker and sending it to the server.

This is the attempt to pass over the local path client-side:

NSData *webData = UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"test.png"];
[webData writeToFile:localFilePath atomically:YES];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:url.absoluteString
   parameters:@{@"path": localFilePath}
     progress:nil
      success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
... //results always in error

This is the backend code snipped receiving the path key (in JavaScript):

var fs = require('file-system');

file {
 data: fs.readFileSync(path),
}

However, I always get errors like this:

Error: ENOENT: no such file or directory, open '/Library/Developer/CoreSimulator/Devices...

Is there a problem in my JS-Code? Or what other approach should I use to get the file path? I'd be happy if someone could help me out here.

P.S. other popular existing StackOverflow answers on getting the file path induce the same error code.

Besfort Abazi
  • 373
  • 3
  • 18

2 Answers2

0

It is not possible. You need to create a Data of that image and upload it using multipart to server. I hope this link would work for you:

How to get UIImage from local url and post the image to server ?

Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
0

Either I've misunderstood the question, or you're trying to do the impossible.

The path on your device (in this case the simulator) isn't accessible to the server, because it only exists on the device.

If you want to pass the actual image across, you need to send the data itself in the request, you can't have the server get it's path and reach over the internet into the device to get it.

The accepted answer to this question is an example of how to attach the image's data to an AFNetworking request. Unfortunately, I don't know enough about the server to help much there but I'd expect you to be able to get it the same way you got your path out of the request, it would just the the raw image data instead of a file path.

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • From https://stripe.com/docs/file-upload: "To upload a file, send a multipart/form-data request to https://files.stripe.com/v1/files. Note that the subdomain files.stripe.com is different than most of Stripe’s API endpoints. The request should specify a purpose and a file. ... The following example uploads a file located at /path/to/a/file.jpg on your local file system with the purpose dispute_evidence: – Besfort Abazi Apr 10 '18 at 10:02
  • var stripe = require("stripe")("sk_test_key"); const fp = fs.readFileSync('/path/to/a/file.jpg'); stripe.fileUploads.create({ file: { data: fp, name: 'file.jpg', type: 'application.octet-stream', }, purpose: 'dispute_evidence', ); " – Besfort Abazi Apr 10 '18 at 10:03
  • I think I am getting confused with the "The following example uploads a file located at /path/to/a/file.jpg on your local file system ..." part. How am I meant to understand this correctly? Thanks for your help! – Besfort Abazi Apr 10 '18 at 10:04
  • I've added another link to my answer which shows an example of how to upload a file using AFNetworking. Hope that helps! – deanWombourne Apr 10 '18 at 10:11
  • Thanks @deanWombourne, I tried that method with AFNetworking. I am getting the error: "TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object." How am I supposed to pass over the data in a form that the server-side code accepts? – Besfort Abazi Apr 10 '18 at 10:42
  • @BesfortAbazi Without some more information, I have no idea (I'm not a javascript/server dev!) Can you give an example of which line of code is throwing that issue? If it's too complicated to fit into a comment, please ask another question on s.o. and link to it from a comment here :) – deanWombourne Apr 10 '18 at 14:50