Ios 为什么文件名会在alamo fire中返回nil,并在swift 3中上载图像?
我想上传多部分图像在我的项目与阿拉莫消防有时没有理由阿拉莫消防文件名将返回零-我如何才能避免这一点Ios 为什么文件名会在alamo fire中返回nil,并在swift 3中上载图像?,ios,swift3,alamofire,filenames,image-uploading,Ios,Swift3,Alamofire,Filenames,Image Uploading,我想上传多部分图像在我的项目与阿拉莫消防有时没有理由阿拉莫消防文件名将返回零-我如何才能避免这一点 let params: Parameters = ["name": "image\(i)"] Alamofire.upload(multipartFormData: { (multipartFormData) in
let params: Parameters = ["name": "image\(i)"]
Alamofire.upload(multipartFormData:
{
(multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(imageUploadingViewController.imageUpload[i], 1.0)!, withName: "myfile", fileName: "file.jpeg", mimeType: "image/jpeg")
for (key, value) in params
{
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to: "http:example.com/api/file?api_token=\(enterViewController.api_token)&id=\(self.postID)",headers:nil)
{ (result) in
switch result {
case .success(let upload,_,_ ):
upload.uploadProgress(closure: { (progress) in
UploadingViewController.progressing = progress.fractionCompleted
self.UpdateState()
if progress.fractionCompleted == 1.0 {
DispatchQueue.main.async {
UploadingViewController.fine = UploadingViewController.fine + 1
}
print(UploadingViewController.fine)
self.checkUploadProgress()
print("OK Finished!")
}
})
由于文件名的原因,应用程序将在此行崩溃
multipartFormData.append(UIImageJPEGRepresentation(UploadingImagesViewController.imageUpload[i], 1.0)!, withName: "myfile", fileName: "file.jpeg", mimeType: "image/jpeg")
记住,有时它会起作用,我不知道为什么会发生这种情况
func sendImageToServerWithURL(_ URLString: URLConvertible, method: HTTPMethod, headers: [String : String]?, parameters: [String: Any]?, imageData : Data?,imageName:String,completionHandler: @escaping CompletionHandler) {
Alamofire.upload(multipartFormData: { (multipartFormData) in
if((imageData) != nil)
{
multipartFormData.append(imageData!, withName:imageName, fileName: "swift_file.png", mimeType: "image/png")
}
for (key, value) in parameters!
{
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to:URLString ,headers : headers)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON { response in
print (response.result)
completionHandler(response)
}
case .failure( _): break
//print encodingError.description
}
}
}
我用这个方法上传图片。我检查是否((imageData)!=nil)上传前的数据值。这可能有帮助 您是否在上载功能中将标题设置为值?是的,请稍候,我将将其添加到我的代码由于CompletionHandler,应用程序不允许我运行您需要检查(值为AnyObject)。数据(使用:String.Encoding.utf8.rawValue)!使用之前的数据值我使用了该值,当图像数据为零时,将使用:String.Encoding.utf8.rawValue)返回!但是当我上传图像到服务器时,服务器只显示文本文件,你只需要检查图像数据值而不是零。您正在使用UIImageJPEGRepresentation(imageUploadingViewController.imageUpload[i]获取imagedata值。如果图像数据值不为零,并且您的服务器发送文本文件,则这是服务器端问题。它可以正常工作,但有时某些图像无法上载,但在我使用您的代码后不会崩溃,谢谢