代码之家  ›  专栏  ›  技术社区  ›  Saeed Rahmatolahi

为什么文件名会在alamo fire中返回nil并在swift 3中上传图像?

  •  0
  • Saeed Rahmatolahi  · 技术社区  · 7 年前

    我想上传多部分图像在我的项目与阿拉莫消防有时没有理由阿拉莫消防文件名将返回零-我怎么能避免?

    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")
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   balkaran singh    7 年前
    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
            }
        }
    }
    

    我用这个方法上传图片。我检查一下 上传前的数据值。这可能会有帮助。