0

I am trying to upload multiple images to server via MultipartFormData

I had read many links, but couldn't solve mine. Code is working when we are uploading single file either .jpg or .png.

In Postman multiple images are uploaded successfully.

Core Logic

let boundaryPrefix = "--\(boundary)\r\n"
body.append("\(boundaryPrefix)".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"\"; filename=\"\(listOfFiles[0].name)\"\r\n".data(using: .utf8)!)
body.append("Content-Type: image/png\r\n\r\n".data(using: .utf8)!)
body.append(convertFileToData(url: listOfFiles[0].pathURL)!)
body.append("\r\n".data(using: .utf8)!)
    
body.append("\(boundaryPrefix)".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"\"; filename=\"\(listOfFiles[1].name)\"\r\n".data(using: .utf8)!)
body.append("Content-Type: image/jpg\r\n\r\n".data(using: .utf8)!)
body.append(convertFileToData(url: listOfFiles[1].pathURL)!)
body.append("\r\n".data(using: .utf8)!)
    
body.append("--".appending(boundary.appending("--")).data(using: .utf8)!)


func convertFileToData(url: URL) -> Data? {
    do {
        let fileData = try Data(contentsOf: url)
        return fileData
    } catch {
        return nil
    }
}


session.dataTask(with: urlRequest) { (dataa, response, error) in
     if error == nil {
         if let _ = dataa {
              do {
                  let val = try JSONDecoder().decode(UploadResponse.self, from: dataa!)
              } catch {
              }
         } else {  }
     } else { }
 }.resume()

Issues:

  • Only first array of Image is reaching server.
  • Second image is not reaching server.

What mistake have I made?

James Z
  • 12,209
  • 10
  • 24
  • 44
McDonal_11
  • 3,935
  • 6
  • 24
  • 55

1 Answers1

0

Try this out:

let boundary = "Boundary-\(UUID().uuidString)"
let boundarySuffix = "--\(boundary)--\r\n"
                
    if let images = files,images.count > 0 {
             let boundaryPrefix = "--\(boundary)\r\n"
             request?.addValue("multipart/form-data; boundary=" + boundary, forHTTPHeaderField: "Content-Type")
             let data = NSMutableData()
             if let params = parameters,params.count > 0{
                for (key, value) in params {
                    data.append("--\(boundary)\r\n".nsdata)
                    data.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".nsdata)
                    data.append("\((value as AnyObject).description ?? "")\r\n".nsdata)
                 }
              }
              for file in images {
                  data.append(boundaryPrefix.nsdata)
                  data.append("Content-Disposition: form-data; name=\"\(file.name!)\"; filename=\"\(NSString(string: file.filename!))\"\r\n\r\n".nsdata)
                   if let a = file.data {
                      data.append(a)
                      data.append("\r\n".nsdata)
                   } else {
                      print("Could not read file data")
                   }
               }
               data.append(boundarySuffix.nsdata)
               request?.httpBody = data as Data
    }
Shivani Bajaj
  • 996
  • 10
  • 23