Swift 上传不带参数的多部分形式的 UIImage
Swift upload UIImage with multipart form without parameters
我使用的服务器接受:
curl -X 'POST' \
'https://some.url' \
-H 'accept: application/json' \
-H 'authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: multipart/form-data' \
-F 'data=@your-image.jpg;type=image/jpeg'
我只能找到带参数的例子。我已经尝试了一切,alamofire 和标准 url 会话
出于某种原因,只有 Alamofire 有效,这是我使用的最后一种方法。
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(image.jpegData(compressionQuality: 0.8)!, withName: UUID().uuidString, fileName: "file.jpeg", mimeType: "image/jpg")
},
to: "https://someurl.com", method: .post , headers: headers)
.uploadProgress(queue: .main, closure: { progress in
//Current upload progress of file
print("Upload Progress: \(progress.fractionCompleted)")
})
.responseJSON(completionHandler: { data in
//Do what ever you want to do with response
print(data)
})
我使用的服务器接受:
curl -X 'POST' \
'https://some.url' \
-H 'accept: application/json' \
-H 'authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: multipart/form-data' \
-F 'data=@your-image.jpg;type=image/jpeg'
我只能找到带参数的例子。我已经尝试了一切,alamofire 和标准 url 会话
出于某种原因,只有 Alamofire 有效,这是我使用的最后一种方法。
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(image.jpegData(compressionQuality: 0.8)!, withName: UUID().uuidString, fileName: "file.jpeg", mimeType: "image/jpg")
},
to: "https://someurl.com", method: .post , headers: headers)
.uploadProgress(queue: .main, closure: { progress in
//Current upload progress of file
print("Upload Progress: \(progress.fractionCompleted)")
})
.responseJSON(completionHandler: { data in
//Do what ever you want to do with response
print(data)
})