Retrieving POST information from forms on UIWebview (image, multipart/form-data)

Asked 2 years ago, Updated 2 years ago, 87 views

This is my first post. Thank you for your cooperation.

The following PHP files are placed on the server and loaded into the UIWebView.

<form action="native://test" method="post" enctype="multipart/form-data">
<input type="file" accept="image/jpeg,image/gif,image/png"name="files"name="photo">
<input type="text" name="name">
...
</form>

In Swift, use the shouldStartLoadWithRequest method to

 vardata:NSData?=request.HTTPBody

Then data is nil.

<input type="file" accept="image/jpeg,image/gif,image/png"name="files"name="photo">

data can be retrieved correctly instead of nil.

What I want to do is to save the image I sent from Form to SQLite. Another method is fine, please let me know.

ios swift xcode6

2022-09-30 19:32

1 Answers

If the amount of data sent is large, it appears that the data is set to HTTPBodyStream instead of HTTPBody.

This is a sample code from HTTPBodyStream to NSMutableData.

 iflet stream=request.HTTPBodyStream{
  stream.open()
  vardata —NSMutableData=NSMutableData()
  whilestream.hasBytesAvailable {
    var buffer = [UInt8] (count:512, repeatedValue:0)
    letlen:Int=stream.read(&buffer, maxLength:buffer.count)
    data.appendBytes (buffer, length:len)
  }
}


2022-09-30 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.