无法使用 Alamofire 从数组上传多个文件
Posted
技术标签:
【中文标题】无法使用 Alamofire 从数组上传多个文件【英文标题】:Cant upload multiple files from array with Alamofire 【发布时间】:2018-09-04 20:49:37 【问题描述】:我正在尝试将多个图像和视频从库上传到 url。 evertying 工作正常,除了,响应只打开一个文件。我选择两个不同的图像进行实验,但响应仅适用于数组中的最后一个图像。这是我的上传功能..
func upload()
var mediaData = Data()
var fname = ""
var mimetype = ""
let url = URL(string: "https://myurl.com/api/attachment/upload")
let headers: HTTPHeaders = [
"Authorization": "my Authorization Token",
"Content-type": "multipart/form-data"
]
Alamofire.upload(multipartFormData: multipartFormData in
for n in 0..<self.array.count
mediaData = self.array[n].data
if self.array[n].type == "image"
fname = "resim\(n).jpeg"
mimetype = "image/jpeg"
else
fname = "video\(n).mp4"
mimetype = "video/mp4"
multipartFormData.append(mediaData, withName: "files", fileName: fname, mimeType:mimetype)
,
to: url!, method: .post, headers: headers) (result) in
switch result
case .success(let upload, _, _):
upload.uploadProgress(closure: (progress) in
print("uploding: \(progress.fractionCompleted)")
)
upload.responseJSON response in
print(response)
self.hud.dismiss()
if let err = response.error
print(err)
return
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
这就是服务器返回的响应。
SUCCESS:
files = (
"local_name" = BaN9x05lU8jNQPJ30RfbT4rqismLp7LE45sHezHvI;
"real_name" = "resim1.jpeg";
);
知道我在哪里犯错了吗?
【问题讨论】:
【参考方案1】:check below code
var dataArray = [("name":"unicorn1","imageData":imgData1),("name":"unicorn2","imageData":imgData2)] 阿拉莫火.上传( multipartFormData: multipartFormData in
for dat in dataArray
multipartFormData.append(dat.imageData, withName: dat.name)
,
to: "https://httpbin.org/post",
encodingCompletion: encodingResult in
switch encodingResult
case .success(let upload, _, _):
upload.responseJSON response in
debugPrint(response)
case .failure(let encodingError):
print(encodingError)
)
【讨论】:
感谢您的考虑,但我的问题是数组不是静态的。可能是一个或十个。 我编辑了答案,如果有帮助,请立即检查,然后请投票 感谢您的关心,但我找到了解决方案。【参考方案2】:我解决了将"files[\(n)]"
更改为代码部分的问题
multipartFormData.append(mediaData, withName: "files", fileName: fname, mimeType:mimetype)
然后问题解决了。
func upload()
var mediaData = Data()
var fname = ""
var mimetype = ""
let url = URL(string: "https://myurl.com/api/attachment/upload")
let headers: HTTPHeaders = [
"Authorization": "my Authorization Token",
"Content-type": "multipart/form-data"
]
Alamofire.upload(multipartFormData: multipartFormData in
for n in 0..<self.array.count
mediaData = self.array[n].data
if self.array[n].type == "image"
fname = "resim\(n).jpeg"
mimetype = "image/jpeg"
else
fname = "video\(n).mp4"
mimetype = "video/mp4"
multipartFormData.append(mediaData, withName: "files[\(n)]", fileName: fname, mimeType:mimetype)
,
to: url!, method: .post, headers: headers) (result) in
switch result
case .success(let upload, _, _):
upload.uploadProgress(closure: (progress) in
print("uploding: \(progress.fractionCompleted)")
)
upload.responseJSON response in
print(response)
self.hud.dismiss()
if let err = response.error
print(err)
return
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
【讨论】:
以上是关于无法使用 Alamofire 从数组上传多个文件的主要内容,如果未能解决你的问题,请参考以下文章