Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用swift 4 use Multi-part&;将图像上载到服务器;阿拉莫菲尔_Ios_Swift_Alamofire_Postman_Nsurlsession - Fatal编程技术网

Ios 使用swift 4 use Multi-part&;将图像上载到服务器;阿拉莫菲尔

Ios 使用swift 4 use Multi-part&;将图像上载到服务器;阿拉莫菲尔,ios,swift,alamofire,postman,nsurlsession,Ios,Swift,Alamofire,Postman,Nsurlsession,我正在尝试使用alamofire将照片从图库上传到服务器,这是如何使用postman上传照片的 这个代码是我的但它失败了不能上传图像, 当试图从postman生成代码时,它也会给我错误,我无法理解代码,这是postman的代码 `let headers = [ "content-type": "multipart/form-data; boundary=---- WebKitFormBoundary7MA4YWxkTrZu0gW", "Content-Type": "application/

我正在尝试使用alamofire将照片从图库上传到服务器,这是如何使用postman上传照片的

这个代码是我的但它失败了不能上传图像, 当试图从postman生成代码时,它也会给我错误,我无法理解代码,这是postman的代码

`let headers = [
"content-type": "multipart/form-data; boundary=---- 
WebKitFormBoundary7MA4YWxkTrZu0gW",
"Content-Type": "application/x-www-form-urlencoded",
"APP_KEY": "{APP_KEY}",
"AUTH_KEY": "{AUTH_KEY}",
"Cache-Control": "no-cache",
]
let parameters = [
[
"name": "img",
"fileName": "C:\Users\Admin\Downloads\IMG-20190130-WA0006(1).jpg"
]
]

 let boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"

 var body = ""
 var error: NSError? = nil
 for param in parameters {
 let paramName = param["name"]!
 body += "--\(boundary)\r\n"
 body += "Content-Disposition:form-data; name=\"\(paramName)\""
 if let filename = param["fileName"] {
 let contentType = param["content-type"]!
 let fileContent = String(contentsOfFile: filename, encoding: 
 String.Encoding.utf8)
 if (error != nil) {
   print(error)
 }
 body += "; filename=\"\(filename)\"\r\n"
 body += "Content-Type: \(contentType)\r\n\r\n"
 body += fileContent
 } else if let paramValue = param["value"] {
 body += "\r\n\r\n\(paramValue)"
 }
 }

 let request = NSMutableURLRequest(url: NSURL(string: 
"https://nearyouweb.com/Api/v1/en/image/upload? 
 model=tbl_accounts&model_id=8")! as URL,
                                    cachePolicy: .useProtocolCachePolicy,
                                timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, 
completionHandler: { (data, response, error) -> Void in
 if (error != nil) {
 print(error)
 } else {
  let httpResponse = response as? HTTPURLResponse
 print(httpResponse)
 }
 })

dataTask.resume()`

首先,直接存储图像是不好的做法,通常建议存储到图像的链接,并使用SDWebImage或Kingfisher之类的库传递图像URL

然后,当向服务器发送数据时,您使用
Alamofire.request…
然后您的请求类型应为
.post
.put


试试看它是否有效。

我认为您还需要在Almaofire上载中使用多部分数据

看到这个了吗

let parameters = [
            // List Parameter here  if any
        ]

Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(imageData, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
            }, to:"yourURL")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
            })

            upload.responseJSON { response in
                //print response.result
            }

        case .failure(let encodingError):
               //print encodingError.description
        }
    }

看,我试图更新个人资料图片,所以我得到了选中的照片,并尝试将其上传到服务器,就像链接中的图像一样,我试图把'alamofire.request'放在call中,但它告诉我额外的参数'to',我在哪里设置标题,从哪里获取照片路径和swift文件,你能说清楚吗thanks@MohammedHassan我认为这将是有益的,我很抱歉,但它也失败了,你能让它像我不知道的那样工作吗参数在哪里,文件名和文件路径是什么等等…很抱歉,它也失败了。你能让它像我不知道参数在哪里,文件名和文件路径是什么一样工作吗?@MohammedHassan明天我会帮你解决这个问题。我今天去了
let parameters = [
            // List Parameter here  if any
        ]

Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(imageData, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
            }, to:"yourURL")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
            })

            upload.responseJSON { response in
                //print response.result
            }

        case .failure(let encodingError):
               //print encodingError.description
        }
    }