0

I want to upload selected image to imagePickerController with absolutely the simplest code that I can understand. (I notice answers to similar questions on stackoverflow are still quite long and not easy to understand without much explanations.) Absolute no codes that are not a must. Hence I wrote the codes below. But no files have been uploaded to my server. Does it look like there are something wrong in my codes below? Or maybe so short that I am actually missing something? Or should I cross-check my server code?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let photoForUpload = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
    var photoData = photoForUpload.jpegData(compressionQuality: 0.8)
    print(type(of: photoData))
    AF.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(photoData!, withName: "fileToUpload")
    }, to: "https://www.mywebsite.com/myphpfile.php").response { response in
        debugPrint(response)
    }
    dismiss(animated: true, completion: nil)
}
Bosco Tsin
  • 11
  • 3
  • `photoData = Data("photoData".utf8)`: You are overriding the file data with the string `"photoData"`... Remove that line. Also, is `debugPrint(response)` called? What's the output? – Larme Jun 06 '22 at 16:51
  • @Larme i just noticed a small bug (out of multiple bugs??)... i need to have `.response` before `{ response in...` let me fix that first and update you more – Bosco Tsin Jun 06 '22 at 16:59
  • @Larme i tried remove the line as you suggested. then i faced another hurdle. the debugPrint(response) told me that my server can only accept jpg file only. Actually I wrote the server code myself and I only allowed jpg data lol. but what i am sending in is something in DATA format? i actually dont know what is DATA format. should i somehow change photoData into jpg format, or better modify my source code to accommodate whatever this DATA format is in Swift? – Bosco Tsin Jun 06 '22 at 17:05
  • Data: That's just "hex/binary/raw" in a high level Swift object... You might need to specify in `multipartFormData.append(photoData!, withName: "fileToUpload")` that's it's a jpg. There should be other method available with a fileExtension parameter I guess... – Larme Jun 06 '22 at 17:07
  • Here: https://github.com/Alamofire/Alamofire/blob/master/Source/MultipartFormData.swift#L150 there is a `mimeType` parameter – Larme Jun 06 '22 at 17:09
  • I tried to add this `mimeType: ".JPG"` after the withName parameter, it still doesn't work... any other obvious point of errors you could see? i could also go check my PHP server code and see if anything wrong on that side... thanks a lot Larme!!! – Bosco Tsin Jun 06 '22 at 17:28
  • Because that's not a mimeType... See https://stackoverflow.com/questions/33692835/is-the-mime-type-image-jpg-the-same-as-image-jpeg And share then `debugPrint(response)` output too... – Larme Jun 07 '22 at 07:56
  • @Larme yes i know now and fixed that. i also added some comments on my PHP code and I realize that in my PHP code there is nothing in my $_FILES["fileToUpload"]["tmp_name"] variable. So that means the PHP file cannot pick up any images... why is that? – Bosco Tsin Jun 07 '22 at 12:10
  • "I don't speak PHP", but `$_FILES["fileToUpload"]` has content? What's "tmp_name" here? – Larme Jun 07 '22 at 12:33
  • ok i guess never mind... just for your information this is a so-called "global variable" in PHP which holds all files that are sent to a php server for handling. supposedly if everything works, the variable above will hold a temporary path to the temporary file on server along with other minor information. and someone the variable shows that no path has been allocated to the temp file. so whether its swift or php problem i am still not so sure, but long story short i think my php codes more likely correct, so likely some problems with my Swift – Bosco Tsin Jun 07 '22 at 13:29

1 Answers1

0

I just want to let every one know that the seeming issue is that I cannot upload a file as large as iPhone photo onto my particular server that I bought the service from third-party provider. I have to compress the image to 0.3 quality with SWIFT before my PHP $_FILES variable gives correct information. Looks like as simple as that.

As I have never seen such a simple answer on StackOverflow or Google, I think it would be good for me to share here. Thanks all for trying.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Bosco Tsin
  • 11
  • 3