1

Inside my application I give the user access to UIImagePickerController allowing them to either take a photo or choose from the library to save to a path in the DocumentDirectory:

        //Obtaining Saving Path
    let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentDirectory: AnyObject = path.objectAtIndex(0)
    let imagePath = documentDirectory.stringByAppendingPathComponent("Photo.png")

    //casting the image selected as a UIImage
    var chosenImage: UIImage = info[UIImagePickerControllerOriginalImage] as UIImage

    //writing it to the set imagePath
    UIImagePNGRepresentation(chosenImage).writeToFile(imagePath, atomically: true)

My aim is to access this file that has been saved from within a WebView in another ViewController, and have run some JS to have the file uploaded automatically when the page finishes loading, opposed to clicking the "Choose File" button and selecting the file etc.

This is the element I want to adjust the value of.

<input type="file" name="field_153" id="field_153">

and this is what what I am running when the web view finishes loading:

func webViewDidFinishLoad(webView: UIWebView) {

//Retreive a UIImage object from disk
let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory: AnyObject = path.objectAtIndex(0)
let imagePath = documentDirectory.stringByAppendingPathComponent("Photo.png")
let savedPhoto: UIImage = UIImage(contentsOfFile: imagePath)!

....
let uploadPictureJS = "var inputFields = document.getElementById('field_153'); inputFields.value = '\(imagePath)';"
self.webView.stringByEvaluatingJavaScriptFromString(uploadPictureJS) }

I've tried using both imagePath and savedPhoto, but this evidently isn't as simple as inputting a text value to the element.

Is this possible to do using JS? Or is there an easier alternative way to accomplish this?

Thanks

Stuart Pattison
  • 173
  • 2
  • 12
  • Browsers don't generally let JavaScript set a file upload path. This protects against malicious sites auto uploading files. Are you trying to do this for all web pages or just certain ones you own? – Brian Nickel Feb 19 '15 at 14:12
  • Hi Brian, I need this to run on 7 different sites. These aren't sites that I own, but my code performs a check of the url before any of the JS is executed. (I also have it completing text fields etc) - Is there any way of checking if it is possible before I waste any more time on this? Thanks – Stuart Pattison Feb 19 '15 at 14:20
  • I can't find a canonical source but all the answers to [this question](http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html) point to no. – Brian Nickel Feb 19 '15 at 14:30
  • You could try an approach where you intercept the post request from the browser and replace it with one you construct with the field values and image but that could be brittle/risky. – Brian Nickel Feb 19 '15 at 14:34

0 Answers0