4

I'm wondering if there is a way to upload a file from an app home directly using a UIWebview that has a HTML form with a file upload input.

I'm loading this from a UIWebview:

<form action="test.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

but the iPhone asks if I'd like to "Take a photo" or "choose existing". What I'd rather do is choose a doc or image that is in the local directory of the App.

Is this possible?

Thanks!

3 Answers 3

1

I too am looking for an answer. After my own digging it seems that the only way is to:

  • intercept when the user clicks the upload button
  • add a custom file picker
  • then send a post request using NSURLConnection instead of submitting the form.
Sign up to request clarification or add additional context in comments.

1 Comment

This is pretty much what I had to do to get this to work. This is the method I did the intercepted and reconfigured the POST. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
0

On iOS, there is no way to "browse the filesystem" so you can't use a form in UIWebView to attach photos for upload.:

You're going to have to -

UIImagePickerController to get a picture from the photos album.

Determine the POST headers for the submit form

Encode the image as NSData

Write a POST request with NSURLConnection with the NSData object getting inserted in the proper header

Comments

0

I was having a similar issue, and I have discovered that the UIWebView elements in IOS do not support the html element:

I am not sure why Apple chose to not support this IMPORTANT html element, but I am sure they have their reasons. (Even though this element works perfectly on Safari on IOS.)

In many cases, when the user clicks this kind of button in a UIWebView, it will let them take/ choose a photo. HOWEVER, the UIWebView in IOS does not have the capability to attach files like this into the POST data when the form is submitted.

The Solution: To accomplish the same task you can create a similar form in InterfaceBuilder with a button that triggers the UIImagePickerController. Then, you create you an HTTP POST request with all of the form data and the image. It isn't as hard as it sounds, check out the link below for some sample code that gets the job done: ios Upload Image and Text using HTTP POST

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.