Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

swift - iOS: How to get a specific string from a Data containing Strings and an image?

I have that code that add values to body:

let body = NSMutableData()
let mimetype = "image/jpg"

//define the data post parameter

body.append("--(boundary)
".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name="eventId"

".data(using: String.Encoding.utf8)!)
body.append("(eventId)
".data(using: String.Encoding.utf8)!)

body.append("--(boundary)
".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name="contactId"

".data(using: String.Encoding.utf8)!)
body.append("(contactId)
".data(using: String.Encoding.utf8)!)

body.append("--(boundary)
".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name="type"

".data(using: String.Encoding.utf8)!)
body.append("(type)
".data(using: String.Encoding.utf8)!)

body.append("--(boundary)
".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name="file"; filename="(fileName)"
".data(using: String.Encoding.utf8)!)
body.append("Content-Type: (mimetype)

".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("
".data(using: String.Encoding.utf8)!)

body.append("--(boundary)--
".data(using: String.Encoding.utf8)!)

request.httpBody = body as Data

How to convert it in string? I would like to get the boundary, but impossible to do it.

When I do that, it makes me nil: let test = String(data: body as Data, encoding: .utf8)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you need your boundary you should create its header and footer data in a separate object from your image data:

let boundary = UUID().uuidString
let eventId = "your event ID"
let contactId = "your contact ID"
let type = "your type string"
let mimetype = "image/jpg"
let fileName = "the file name"
let imageData = Data() // your image data
let boundaryHeader = Data("""
--(boundary)

Content-Disposition:form-data; name="eventId"


(eventId)

--(boundary)

Content-Disposition:form-data; name="contactId"


(contactId)

--(boundary)

Content-Disposition:form-data; name="type"


(type)

--(boundary)

Content-Disposition:form-data; name="file"; filename="(fileName)"

Content-Type: (mimetype)


""".utf8)

let boundaryFooter = Data("""


--(boundary)--

""".utf8)

let body = boundaryHeader + imageData + boundaryFooter

var request = URLRequest(url: URL(string: "http://www.example.com/whatever")!)
request.httpBody = body

print(String(data: boundaryHeader, encoding: .utf8) ?? "nil")

This will print:

Content-Disposition:form-data; name="contactId"

your contact ID

--2583374D-68AF-4EE1-96A5-740CCA17C51D

Content-Disposition:form-data; name="type"

your type string

--2583374D-68AF-4EE1-96A5-740CCA17C51D

Content-Disposition:form-data; name="file"; filename="the file name"

Content-Type: image/jpg


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...