繁体   English   中英

将图像上传到服务器,参数错误 swift

[英]Uploading image to server with parameter error swift

我正在尝试使用文件夹名称、文件名和扩展名作为参数将图像上传到服务器。 我已经完成了一些代码,但它给了我一个 Nil 响应。 有人可以帮我解决这个问题吗? 有时这会上传图像,有时不会。 当它上传图片失败时会给出一个 Nil 响应。并向服务器发送一些垃圾值。 这是我的图片上传方法:

func UPLOD(){
    let image  = myImageView.image!
    let serviceName = "http://192.168.80.21:8800/api/v1/upload/uploadfile"
    var parameters = [String: AnyObject]()
    parameters["Folder"] = "uploadfile" as AnyObject?
    parameters["Filename"] = "demo\(self.currentTimeStamp)" as AnyObject?
    parameters["Ext"] = "jpg" as AnyObject?
    parameters["FileToUpload"] = image.jpegData(compressionQuality: 0.5) as AnyObject?

    guard let token = UserDefaults.standard.string(forKey: "accesstoken") else {
        return
    }
    print("Create button ACCESS KEY::::- \(token)")
    let headers: HTTPHeaders = [
        "x-access-token": token
    ]
    
    Alamofire.upload(multipartFormData: { (multipartFormData:MultipartFormData) in
        for (key, value) in parameters {
            if key == "FileToUpload" {
                multipartFormData.append(
                    value as! Data,
                    withName: key,
                    fileName: "demo\(self.currentTimeStamp)",
                    mimeType: "image/jpg"
                    //_img.jpg
                )
            } else {
                //Data other than image
                multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
            }}},
    to: serviceName, method: .post, headers: headers) { (encodingResult:SessionManager.MultipartFormDataEncodingResult) in
        switch encodingResult {
        
        case .success(let upload, _, _):
            upload.responseJSON { response in
                            //print response.result
                print(response.result.value as Any)
                        }
            upload.responseJSON { [self] response in
                
                if let Response = response.result.value as? [String : Any],
                   let myData = Response["data"] as? [String : Any],
                   let imgPath = myData["ImagePath"]  {
                    imageUrl = imgPath as! String
                    print(imageUrl)
                    print("ImagePath --> ", imgPath)
                    responseURL = imageUrl
                    let defaults = UserDefaults.standard
                    defaults.setValue(imageUrl, forKey: "imageURL")
                    let key = defaults.object(forKey: "imageURL")
                    print(key as Any)
                    self.alamofireRequest(requestURL: "http://192.168.80.21:3204/api/product/create")
                }
                if let data = response.result.value {
                    let _ = JSON(data)
                }
            }
            break
            
        case .failure(let encodingError):
            print(encodingError)
            break
        }
    }
}

Swift 5.0

pod 'Alamofire', '~> 5.4'

func postImageData(url:String, param:[String:Any], img:Data) {
        print("POST URL : ", url)
        let header: HTTPHeaders = [
            "Content-type": "multipart/form-data"
        ]
        AF.upload(multipartFormData: { (MultipartFormData) in
            MultipartFormData.append(img, withName: "image", fileName: "image", mimeType: "image/jpeg")
            for (key,value) in param {
                MultipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
            }
        }, to: url, method: .post, headers: header).uploadProgress { (progress) in
            print(progress.fractionCompleted)
        } .responseJSON { response in
            switch response.result {
            case .success:
                if let JSON = response.value as? [String: Any] {
                    let flag = JSON["flag"] as! Bool
                    let code = JSON["code"] as! Int
                    
                } else {
                    
                }
                break
            case .failure(let error):
                if let data = response.data {
                    print("Response Error Line No. 265:- \(NSString(data: data, encoding: String.Encoding.utf8.rawValue)!)")
                }
                break
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM