简体   繁体   English

如何在Swift4中通过多参数的多部分表单数据上传图像或文件

[英]How to upload image or file through multipart form data with multi parameters in Swift4

I have a problem uploading a picture or file through image picker controller.我在通过图像选择器控制器上传图片或文件时遇到问题。 I have selected image through image picker then pass the parameters event_id, booking_id, memeber_id, and upfile.我通过图像选择器选择了图像,然后传递了参数 event_id、booking_id、memeber_id 和 upfile。 All these values are in the form-data format in Postman.所有这些值都采用 Postman 中的表单数据格式。 How to send these value to API?如何将这些值发送到 API? In postman, it's running perfectly but here in Xcode its showing error.在邮递员中,它运行良好,但在 Xcode 中显示错误。 I am thinking the parameters, I am sending in that error contains.我在考虑参数,我发送的错误包含。

func UploadRequest() {
    let x: Int = defaults.integer(forKey: "ObjectID")
    user_id = String(x)
    auth_key = defaults.value(forKey: "AuthKey") as! String
    var request2 = URLRequest(url: uploadDoc! as URL)
    request2.httpMethod = "POST"

    //define the multipart request type
    if (self.img.image == nil) {
        return
    }
    let image_data = UIImageJPEGRepresentation(self.img.image!, 1)
    if(image_data == nil) {
        return
    }
    let boundry = generateBoundaryString()
    request2.setValue(user_id, forHTTPHeaderField: "user_id")
    request2.setValue(auth_key, forHTTPHeaderField: "auth_key")
    request2.setValue("multipart/form-data; boundary=\(boundry)", forHTTPHeaderField: "Content-Type")
    let param = ["event_id":evId,"booking_id":receivedId,"member_id":memId]
    print(param)
    request2.httpBody = createBody(params: param, filePathKey: "upfile", imageDatakey: image_data! as NSData, boundary: boundry) as Data
    self.jsonDifferentRunDoc(passString: request2)
}

//Generate Boundry String
func generateBoundaryString() -> String {
    return "Boundary-\(UUID().uuidString)"
}

func createBody(params: [String: String]?, filePathKey: String, imageDatakey: NSData, boundary: String) -> NSData {
    let body = NSMutableData()
    if params != nil {
        for (key, value) in params! {
            body.appendString(boundary)
            body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.appendString("\(value)\r\n")
        }
    }

    let filename = "jk.jpg"
    let mimetype = "image/jpg"

    body.appendString(boundary)
    body.appendString("Content-Disposition: form-data; name=\"upfile\"; filename=\"\(filename)\"\r\n")
    body.appendString("Content-Type: \(mimetype)\r\n\r\n")
    body.append(imageDatakey as Data)
    body.appendString("\r\n")
    body.appendString("--".appending(boundary.appending("--")))

    //print("Body = \(body)")
    return body
}

It is showing error when I run this function.运行此函数时显示错误。

There is syntax error while uploading boundary:上传边界时出现语法错误:

lineBreak = "\r\n";
body.appendString("--\(boundary)--\(lineBreak)");

Please try this.请试试这个。

暂无
暂无

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

相关问题 如何在iOS,Swift3,Alamofire 4中使用多部分表单数据将图像作为参数上传以及其他参数 - How to upload image as a parameter, with other parameters using multipart form data in ios, swift3, Alamofire 4 swift4中的alamofire中的多部分表单数据请求? - multipart form data request in alamofire in swift4? 如何在 Swift 中使用 Codable 和 URLSession.shared.uploadTask (multipart/form-data) 上传图像文件? - How to upload image file using Codable and URLSession.shared.uploadTask (multipart/form-data) in Swift? 如何在Swift中使用嵌套的JSON参数上传多部分表单数据? - How can we upload multipart form data with nested JSON parameters in Swift? Swift - 包含和不包含图像的multipart / form-data POST请求(上传) - Swift - multipart/form-data POST request(upload) with and without image 在 Swift 中上传带有多部分表单数据 iOS 的图像 - Upload image with multipart form-data iOS in Swift 仅在 Swift 4.2 中上传带有多部分表单数据的图像 - Upload image with multipart form-data only in Swift 4.2 我想使用 alamofire swift 5 将图像上传到多部分表单数据 - I want to upload image to multipart form data using alamofire swift 5 在Swift中上传包含多部分/表单数据的图像和参数 - Uploading an image and parameters with multipart/form-data in Swift 在Swift 4中的Alamofire 4中使用multipart上传图像和多个参数 - upload image and multiple parameters using multipart in alamofire 4 in swift 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM