【问题标题】:I cant get parameters to pass with image in Alamofire 4.0 multipartform upload我无法在 Alamofire 4.0 多部分上传中获取参数以与图像一起传递
【发布时间】:2017-06-18 14:29:30
【问题描述】:

我无法通过多部分数据上传读取 php 中的参数。我正在使用 Alamofire 4 和 swift 3。下面是我的代码:

var parameters:[String: String]
                parameters = [
            "ID": self.fieldNote.ID,
            "createdBy": self.fieldNote.createdBy,
            "workOrderID": self.fieldNote.workOrderID,
            "customerID": self.fieldNote.customerID,
            "note"      : self.fieldNote.note,
            "status":"0",
            "imageEdit":String(self.imageEdit)
        ]

    print("parameters = \(parameters)")

    Alamofire.upload(
        multipartFormData: { multipartFormData in


                for (key, value) in parameters {
                    multipartFormData.append(value.data(using: .utf8)!, withName: key)
                }
                multipartFormData.append(UIImageJPEGRepresentation(self.imageView.image!, 1)!, withName: "pic", fileName: "user.jpg", mimeType: "image/jpeg")

    },
        to: "http://www.___.com/cp/app/functions/upload.php",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint("SUCCESS RESPONSE: \(response)")
                }
                upload.responseString { response in
                    debugPrint("SUCCESS RESPONSE: \(response)")
                }
            case .failure(let encodingError):
                print("ERROR RESPONSE: \(encodingError)")

            }
        }
    )

php:

    foreach ($_FILES as $key => $value) {
        echo "<tr>";
        echo "<td>";
        echo '$key = ' . $key;
        echo "</td>";
        echo "<td>";
        echo '$value = ' . $value;
        echo "</td>";
        echo "</tr>";
        }

在继续 php 工作之前尝试回显传递的参数。 任何帮助都会很棒。 谢谢

【问题讨论】:

    标签: php upload swift3 alamofire multipartform-data


    【解决方案1】:

    我想出了有效的语法。

     var parameters:[String:String]
            parameters = [
                "ID": self.fieldNote.ID,
                "createdBy": self.fieldNote.createdBy,
                "workOrderID": self.fieldNote.workOrderID,
                "customerID": self.fieldNote.customerID,
                "note"      : self.fieldNote.note,
                "status":"0",
                "imageEdit":String(self.imageEdit)
            ]
    
    
        print("parameters = \(parameters)")
    
        let URL = try! URLRequest(url: "http://www.___.com/cp/app/functions/upload.php", method: .post, headers: nil)
    
        Alamofire.upload(multipartFormData: { (multipartFormData) in
            print("alamofire upload")
    
            if(self.imagePicked == true || self.imageEdit == true){
    
                multipartFormData.append(UIImageJPEGRepresentation(self.imageView.image!, 1)!, withName: "pic", fileName: "swift_file.jpeg", mimeType: "image/jpg")
            }
    
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        }, with: URL, encodingCompletion: { (result) in
    
           print("result = \(result)")
            switch result {
            case .success(let upload, _, _):
    
                upload.uploadProgress(closure: { (Progress) in
                    print("Upload Progress: \(Progress.fractionCompleted)")
                })
    
                upload.responseJSON { response in
                    print(response.request ?? "")  // original URL request
                    print(response.response ?? "") // URL response
                    print(response.data ?? "")     // server data
                    print(response.result)   // result of response serialization
                    if let JSON = response.result.value {
                        print("JSON: \(JSON)")
                    }
                }
    
                upload.responseString { response in
                    debugPrint("SUCCESS RESPONSE: \(response)")
                }
    
            case .failure(let encodingError):
                print(encodingError)
                            }
        })
    

    php:

        $ID = intval($_POST['ID']);
        $note = $db->real_escape_string($_POST['note']);
        $customerID = intval($_POST['customerID']);
        $woID = intval($_POST['workOrderID']);
        $woItemID = intval($_POST['workOrderItemID']);
        $createdBy = intval($_POST['createdBy']);
        $status = intval($_POST['status']);
    
        $imageEdit = boolval($_POST['imageEdit']);
    
        // check $_FILES['pic'] not empty
        if(!isset($_FILES['pic']) || !is_uploaded_file($_FILES['pic']['tmp_name']))
        {
            $hasImg = false;
        } else {
            $hasImg = true;
        }
    

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      相关资源
      最近更新 更多