问题
I'm using Alamofire 4.0 to upload video to server after select or record it through device/camera but when I'm trying to call upload function with append , this error appeared to me in all append statement , what is the wrong in my code.
the second my question about if I want to display progress with percentage of progress during upload how I can make that through Alamofire.
Thanks :)
My code after reading url of selected/recorded video
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType:AnyObject? = info[UIImagePickerControllerMediaType] as AnyObject?
if let type:AnyObject = mediaType {
if type is String {
let stringType = type as! String
if stringType == kUTTypeMovie as String {
let urlOfVideo = info[UIImagePickerControllerMediaURL] as? NSURL
if let url = urlOfVideo {
// 2
print(url)
let URL = try! URLRequest(url: "myurl", method: .post, headers: ["Authorization": "auth_token"])
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(url, withName: "videoFile", fileName: "filename", mimeType: "mov")
multipartFormData.append("video", withName: "load")
multipartFormData.append("record", withName: "type")
}, with: URL, encodingCompletion: { (result) in
// code
})
}
}
}
}
picker.dismiss(animated: true, completion: nil)
}
回答1:
Regarding the error message:
Cannot invoke '
append' with an argument list of type '(String, withName: String)'
If we take a look at the append methods of MultipartFormData of Alamofire:
- Alamofire/Source/MultipartFormData.swift
We note that no append(...) method allows a first argument of type String, which is what you, however, attempt to use when appending in your multipartFormData closure of Alamofire.upload.
multipartFormData.append("video", withName: "load") multipartFormData.append("record", withName: "type")
I believe you're attempting to use the following method:
public func append(_ data: Data, withName name: String) { ... }
In which case you need to encode your string into the Swift type Data, e.g. as follows:
multipartFormData.append("video".data(using: .utf8)!, withName: "load")
multipartFormData.append("record".data(using: .utf8)!, withName: "type")
As for your call:
multipartFormData.append(url, withName: "videoFile", fileName: "filename", mimeType: "mov")
The immutable url above is of of type NSURL. In Swift 3, you should prefer using Foundation type URL instead, which bridge to NSURL, but is not the same type. We see in Alamofire 4 that it particularly expects the URL type for the append function you attempt to call above:
public func append(_ fileURL: URL, withName name: String, fileName: String, mimeType: String)
You've noted yourself that you may use a workaround to call this method by using the absoluteURL property of NSURL upon your instance url; but this simply yields an optional of type URL. A better approach would simply be using the URL type rather than NSURL from the start.
回答2:
We can do this.
static func requestMultiPartFromData(image: UIImage, fileName: String){
let url = "Your URL HERE"
Alamofire.upload(
multipartFormData: { multipartFormData in
guard let image = image.toString() else { return }
multipartFormData.append(image.data(using: .utf8)!, withName: fileName)
},
to: url,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
}
}
With Extension
extension UIImage {
func toString() -> String? {
let data: Data? = UIImage.pngData(self)()
return data?.base64EncodedString(options: .endLineWithLineFeed)
}
}
来源:https://stackoverflow.com/questions/41401913/cannot-invoke-append-with-an-argument-list-of-type-string-withname-string