gpt4 book ai didi

ios - 在 SWIFT 中上传带有其他参数的图像

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:36 25 4
gpt4 key购买 nike

我想使用 SWIFT 将图像和其他参数上传到服务器,服务器端脚本是 PHP。我将请求发布到 http://www.23look.com/merchant/verify它接受以下参数:

mer_name - 字符串

mer_tel - 字符串

mer_address - 字符串

lng - 字符串

纬度 - 字符串

mer_licence - 文件类型

token - 字符串

mer_type - 字符串

每当我发出 HTTP post 请求时,它总是返回 0 值作为响应,这意味着“系统错误”。也许我做错了什么,有人可以帮我看看我的代码吗?

func uploadImage()
{
let postPictureUrl = NSURL(string: "http://www.23look.com/merchant/verify")
let request = NSMutableURLRequest(URL: postPictureUrl!)
request.HTTPMethod="POST"

let param=[
"mer_name" : shopNameUITF.text!,
"mer_tel" : shopTelephoneUITF.text!,
"mer_address" : shopAddressUITF.text!,
"lat" : "39.6892",
"lng" : "115.9239",
"token": KeychainWrapper.stringForKey("tokenValue")!,
"mer_type": "smalll"
]

let abc = KeychainWrapper.stringForKey("tokenValue")!

let boundary = generateBoundaryString()

request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

let imageData = UIImageJPEGRepresentation(myImageView.image!, 0.1)

if imageData==nil { print("image data is nil"); return }

request.HTTPBody = createBodyWithParameters(param, filePathKey: "mer_license", imageDataKey: imageData!, boundary: boundary)

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in

if error != nil {
print("error=\(error)")
return
}

//You can print out response object
print("***** response = \(response)")

// Print out reponse body
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("**** response data = \(responseString!)")

do {

let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

dispatch_async(dispatch_get_main_queue(), {
print(NSString(data: data!, encoding:NSUTF8StringEncoding)!)
print(json)
})

} catch let err {
print(err)
}
}
task.resume()
}

func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().UUIDString)"
}

func createBodyWithParameters(parameters:[String:String]?, filePathKey: String?, imageDataKey:NSData, boundary: String) -> NSData {

var body=NSMutableData()

if parameters != nil {
for(key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}

let filename = "user-profile.jpg"
let mimetype = "image/jpg"

body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString("Content Type: \(mimetype)\r\n\r\n")
body.appendData(imageDataKey)
body.appendString("\r\n")

body.appendString("--\(boundary)--\r\n")

return body
} extension NSMutableData {
func appendString(string: String) {
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
appendData(data!)
}

最佳答案

如果您使用 appendData 而不是 appendString 会怎样:

let body = NSMutableData()
let tempData = NSMutableData()
for (key, value) in parameters {
tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData("\(value!)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
// etc etc
}

body.appendData(tempData)

request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
request.HTTPBody = body

关于ios - 在 SWIFT 中上传带有其他参数的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35008594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com