【问题标题】:Uploading multiple images(with parameters) using multipart form data - Alamofire 5使用多部分表单数据上传多个图像(带参数) - Alamofire 5
【发布时间】:2023-03-23 06:19:01
【问题描述】:

我一直在尝试使用 alamofire 上传多个带有参数的图像 (3),但我似乎做不到。(我缺乏知识)。有人可以帮我解决这个问题吗? 这是我尝试过的

{
        let headers: HTTPHeaders = [
            xyz
        ]

    let param : [String: Any] = [
        "emp_Id" : "",
        "device_Identifier" : "",
        "timestamp" : "",
        "description" : "",
        "handoverStatus" : ""
    ]
    

            AF.upload(
                multipartFormData: { multipartFormData in
                    multipartFormData.append(imgData0, withName: "media1" , fileName: "file0.jpeg", mimeType: "image/jpeg")
                    multipartFormData.append(imgData1, withName: "media2",fileName: "file1.jpg", mimeType: "image/jpg")
                    multipartFormData.append(imgData2, withName: "media3",fileName: "file1.jpg", mimeType: "image/jpg")
                    // I think im supposed to add the last part here but i dunno how to do that
            },
                to: "http://ip.here.--.--/new.php", method: .post , headers: headers)
                .response { resp in
                    print(resp)

            }
    }

这是服务器所期望的

[{"key":"media1","description":"","type":"file","value":["/C:/Users/x/x/Saved Pictures/x.jpg"]},
[{"key":"media2","description":"","type":"file","value":["/C:/Users/x/x/Saved Pictures/x.jpg"]},
[{"key":"media3","description":"","type":"file","value":["/C:/Users/x/x/x.jpg"]},

{"key":"model","value":"{\"emp_Id\": \"6\",\"device_Identifier\":\"Device 01\",\"timestamp\":\"\123\,
”\description\”:\”description\",”handoverStatus”:”false”}","type":"text"}]

我不知道如何将最后一部分添加到多部分表单数据中,能否指出我正确的方向?

谢谢

【问题讨论】:

    标签: swift alamofire alamofire-upload


    【解决方案1】:

    试试这个方法上传多张图片

    class func uploadImageWithURL(endPath : String, dictImage : [String:[UIImage]], parameter : [String : Any], header : HTTPHeaders? = nil, success : @escaping ( Any? )->Void, failure : @escaping (Error) -> Void){
        
        let baseUrl = "your base URL"
        let fullUrl = baseUrl + endPath
        var headers = ["Content-type" : "multipart/form-data"]
        if let header = header{
            headers = header
        }
        
        let url = (try? URLRequest(url: fullUrl, method: .post, headers: headers))!
        upload(multipartFormData: { (multipartData) in
            
            for imagesData in dictImage {
                
                for arrimage in imagesData.value{
                    
                    multipartData.append(arrimage.jpegData(compressionQuality: 0.50)!, withName: imagesData.key, fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpeg")
                }
                
            }
            for (key, value) in parameter {
                multipartData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
            }
            
        }, with: url) { (resultData) in
            
            switch resultData{
            case .success(let upload, let streamingFromDisk, let fileURL):
                
                print("File URL : \(String(describing: fileURL))")
                print("Streaming From Disk : \(streamingFromDisk)")
                
                upload.uploadProgress(closure: { (progress) in
                    print("Progress : \(progress.fractionCompleted)")
                })
                upload.responseJSON(queue: nil, options: .allowFragments, completionHandler: { (response) in
                    if let value = response.result.value
                    {
                        success(value)
                    }
                })
            case .failure(let error):
                failure(error)
                print(error)
            }
        }
    }
    

    【讨论】:

    • 为什么在'with: url) {(resultData) in' 行上一直说'变量在其自己的初始值内使用' ???? (网址下的错误)
    • @Zyfe3r 更改 alamofire upload 方法与您的 alamofire 框架 OR url 变量重新声明问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多